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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:28:38+00:00 2026-05-18T02:28:38+00:00

In response to .. some other question somewhere, I wrote this code. struct no_type{};

  • 0

In response to .. some other question somewhere, I wrote this code.

struct no_type{};
template<typename T> struct has_apply {
    static decltype(T().apply<0u>(double())) func( T* ptr );
    static no_type func( ... );
    static const bool result = !std::is_same<no_type, decltype(func(nullptr))>::value;
};

class A {
public:
    template< unsigned n >
    void apply( const double& );

};
class B {
};

int main()
{
  std::cout << std::boolalpha << has_apply< A >::result << '\n';
  std::cout << std::boolalpha << has_apply< B >::result << '\n';
  std::cin.get();
  return( 0 );
}

Now it seems to me that result should be true if T offers a non-static member function “apply” that accepts a double rvalue and a template parameter literal, and false otherwise. However, the example given actually fails to compile for class B, when compiling has_apply<B>. Shouldn’t the fact that the substitution of T failed in the decltype statement mean that it simply calls the other func? Isn’t that kind of the point of SFINAE?

Solved in the most ridiculous, pointless fashion ever:

struct no_type{};
template<typename T> struct has_apply {
    template<typename U> static decltype(U().apply<0u>(double())) func( U* );
    template<typename U> static no_type func( ... );
    static const bool result = !std::is_same<no_type, decltype(func<T>(nullptr))>::value;
};

class A {
public:
    template< unsigned n >
    void apply( const double& );

};
class B {
};

int main()
{
  std::cout << std::boolalpha << has_apply< A >::result << '\n';
  std::cout << std::boolalpha << has_apply< B >::result << '\n';
  std::cin.get();
  return( 0 );
}
  • 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-18T02:28:39+00:00Added an answer on May 18, 2026 at 2:28 am

    SFINAE applies when substitution fails for a function template’s template parameter, not for a class template’s template parameter that has the (non-template) function in question as a member, as is in your case.

    After fixing that, you should at least change decltype(T().apply<0u>(double())) to decltype(T().template apply<0u>(double())) because T() expression is of a dependent type. The reason for that is this: when the compiler first sees T().apply<0u>, it doesn’t know anything about T yet, so how should it parse the tokens apply and < after .? apply might be a member template, and then < would start the argument list for it. OTOH apply might instead be a non-template member (e.g. a data member), and then < would be parsed as ‘less-than’ operator. There is an ambiguity, and it’s still too early for the compiler to resolve that at this point. There is a need for a disambiguation mechanism a programmer could use to tell the compiler what apply is expected to be: a template or not. And here comes the .template (or ->template, or ::template) construct to the rescue: if it’s present, the compiler knows it should be a template member, otherwise if it’s not present then the compiler knows the member shouldn’t be a template.

    Finally here’s an example I created that works correctly and produces the desired results on g++ 4.5.0 with -std=c++0x:

    #include <iostream>
    
    template < class T >
    decltype( T().template apply< 0u >( double() ) ) f( T &t )
    {
        return t.template apply< 0u >( 5. );
    }
    
    const char *f( ... )
    {
        return "no apply<>";
    }
    
    class A {
    public:
        template < unsigned >
        int apply( double d )
        {
            return d + 10.;
        }
    };
    
    class B {};
    
    int main()
    {
        A a;
        std::cout << f( a ) << std::endl;
        B b;
        std::cout << f( b ) << std::endl;
    }
    

    Output is:

    15
    no apply<>
    

    Now if you remove both .template from the first f() definition, then the output becomes:

    no apply<>
    no apply<>
    

    Which is to indicate substitution failure for class A as it doesn’t have any non-template member named apply. SFINAE in action!

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

Sidebar

Related Questions

I have read some different responses for this same question, so I dont know
Pre-warning : There are some other questions similar to this but don't quite answer
I have a plist where I would like to save a some response details
I am using a data contract in WCF, but there is some unwanted response
For some reason the facebook crawler is triggering the json response in my rails
There are some double fields in the response have values as NaN, however, i
I got some weird error with response.redirect() and the project wasn't building at all..
I need to select some values from a json response. Im using json.net, fine
As title says for some purpose I need to get the size of request/response
I used Response.Write to write dynamic javascript into a page to test some values

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.