Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8216161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:02:04+00:00 2026-06-07T12:02:04+00:00

I have been having some inexplicable SFINAE problems in a program I’m writing, so

  • 0

I have been having some inexplicable SFINAE problems in a program I’m writing, so I boiled it down to a freestanding example program:

#include <type_traits>

struct Base { };

struct Derived : public Base { };

template<typename T>
struct From { };

template<typename T>
struct To {
    template<typename U>
    To(const From<U>& other) {
        static_assert(std::is_convertible<U*, T*>::value, "error");
    }
};

int main() {
    From<Derived> a;

    To<Base> b = a;
}

This program compiles without error or warning. However, this:

#include <type_traits>

struct Base { };

struct Derived : public Base { };

template<typename T>
struct From { };

template<typename T>
struct To {
    template<typename U>
    To(const From<typename std::enable_if<true, U>::type>& other) {
    // this       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        static_assert(std::is_convertible<U*, T*>::value, "error");
    }
};

int main() {
    From<Derived> a;

    To<Base> b = a;
}

Gives the following error:

test.cpp: In function int main():

test.cpp:22:18: error: conversion from From<Base> to non-scalar type To<Derived> requested

Which is because the substitution fails I presume, and the constructor isn’t seen.

Am I doing SFINAE wrong, or is this a compiler bug? I am using rubenvb’s GCC 4.7.1 on Windows (with std=c++11 if it makes a difference).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T12:02:05+00:00Added an answer on June 7, 2026 at 12:02 pm

    I’d use a default template argument, that way the argument can be deduced:

    #include <type_traits>
    
    struct Base { };
    
    struct Derived : public Base { };
    
    template<typename T>
    struct From { };
    
    template<typename T>
    struct To {
        template<typename U, class = typename std::enable_if<true, U>::type>
        To(const From<U>& other) {
        // this       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
            static_assert(std::is_convertible<U*, T*>::value, "error");
        }
    };
    
    int main() {
        From<Base> a;
    
        To<Derived> b = a;
    }
    

    Note that that causes the static_assert to fail since you are using std::is_convertible the other way around. It should be:

    static_assert(std::is_convertible<T*, U*>::value, "error");
    

    In your example, the template type U can’t be deduced. In my code, it can be deduced, since it is being used as a template argument for the other argument in the constructor. In your code, the compiler sees a std::enable_if<true, U>::type and can’t deduce what that U type is. The fact that the result of that enable_if is being used as a template argument for From doesn’t help at all, since U needs to be deduced before that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi, i have been having some problems using JSON data in flash builder lately
I have been having some problems with downward type conversion in C++ using pointers,
I have been having some problems with the test debugger in VS10. All tests
I have been having some problems trying to get my PHP running. When I
I have been having some problems with this for a few days now... I
I have a PHP application that I have been having some problems with, some
I'm working on http://lobounce.com.s138810.gridserver.com/ and have been having some problems with the slider which
I have been having some problems with some variables inside a class being deleted
I have been having some problems with mp4 video on Android . The codec
We have been having some debate this week at my company as to how

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.