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 6178843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:32:05+00:00 2026-05-24T00:32:05+00:00

Given: class Hokey { public: explicit C(int i): i_(i) { } template<typename T> T&

  • 0

Given:

class Hokey
{
public:
    explicit C(int i): i_(i) { }

    template<typename T>
    T& render(T& t) { t = static_cast<T>(i_); return t; }
private:
    unsigned i_;
};

If I try:

Hokey h(1);
string s;
h.render(s);

Codepad gives me an error on the static cast:

t.cpp: In member function 'T& Hokey::render(T&) [with T = std::string]':
t.cpp:21:   instantiated from here
Line 11: error: no matching function for call to 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(unsigned int&)'

It seems like it should say that there is no Hokey::render to match.

Of course, if I supply a valid overload, everything works.
But given the code below, you uncomment the line, codepad chokes again:

string& render(string& s) const {
    ostringstream out;
    out << i_;
//  s = out.str();
    return s;
}

Doesn’t SFINAE say that – in the first case – the problem within render should not be the error, rather the absence of a render that works should be the error?

  • 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-05-24T00:32:06+00:00Added an answer on May 24, 2026 at 12:32 am

    It’s only not an error during overload resolution. In other words, it puts off giving you an error until it’s sure the call definitely isn’t going to work. After that, it’s an error.

    struct example
    {
        template <typename T>
        static void pass_test(typename T::inner_type); // A
    
        template <typename T>
        static void pass_test(T); // B
    
        template <typename T>
        static void fail_test(typename T::inner_type); // C
    };
    
    int main()
    {
        // enumerates all the possible functions to call: A and B
        // tries A, fails with error; error withheld to try others
        // tries B, works without error; previous error ignored
        example::pass_test(5);
    
        // enumerates all the possible functions to call: C
        // tries C, fails with error; error withheld to try others
        // no other functions to try, call failed: emit error
        example::fail_test(5);
    }
    

    It should also be noted that overload resolution (and therefore SFINAE) only looks at the function signature, not the definition. So this will always fail:

    struct example_two
    {
        template <typename T>
        static int fail_test(T x)
        {
            return static_cast<int>(x);
        }
    
        template <typename T>
        static int fail_test(T x)
        {
            return boost::lexical_cast<int>(x);
        }
    };
    
    int main()
    {
        example_two::fail_test("string");
    }
    

    There are no errors for either template substitution — for the function signatures — so both functions are okay to call, even though we know the first one will fail and the second won’t. So this gives you an ambiguous function call error.

    You can explicitly enable or disable functions with boost::enable_if (or std::enable_if in C++0x, equivalent to boost::enable_if_c). For example, you might fix the previous example with:

    struct example_two_fixed
    {
        template <typename T>
        static boost::enable_if<boost::is_convertible<T, int>, int>
            pass_test(T x) // AA
        {
            return static_cast<int>(x);
        }
    
        template <typename T>
        static boost::disable_if<boost::is_convertible<T, int>, int>
            pass_test(T x) // BB
        {
            return boost::lexical_cast<float>(x);
        }
    };
    
    struct empty {} no_conversion;
    
    int main()
    {
        // okay, BB fails with SFINAE error because of disable_if, does AA
        example_two::pass_test(5);
    
        // okay, AA fails with SFINAE error because of enable_if, does BB
        example_two::pass_test("string");
    
        // error, AA fails with SFINAE, does BB, fails because cannot lexical_cast
        example_two::pass_test(no_conversion);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given class Foo template <typename T> class Foo { public: ...other methods.. void bar()
i have been given class with int variables x and y in private, and
Given class: class C { public: C() { cout << Dflt ctor.; } C(C&
assume the following class is given: class Base{ public: Base() {} Base( const Base&
I have class Foo with a constructor as given: class Foo { public: Foo(int
I have declared a private field and a public property for a given class.
Given class: class SomeClass { public int intdata {get;set;} public string stringdata {get;set;} public
Given class: public class FooAmounts int Id decimal Month1 decimal Month2 ... decimal Month12
Given: class A { public void m(List l) { ... } } Let's say
I have constructed a map and try to put subclasses of a given class

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.