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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:00:05+00:00 2026-06-06T21:00:05+00:00

It seems that using decltype with SFINAE enable_if isn’t straightforward. I tried writing the

  • 0

It seems that using decltype with SFINAE enable_if isn’t straightforward. I tried writing the go using enable_if in three different ways. All of them failed with a compiler error (With GCC literally: “error: ‘thing’ is not a member of ‘foo'” and an instantiation context).

#include <type_traits>

struct foo {
  enum { has_thing = false };
};

struct bar {
  enum { has_thing = true };
  static int thing() { return 0; }
};

template <typename T>
struct Test {
  /*auto go(typename std::enable_if<T::has_thing, int>::type=0) 
  -> decltype(T::thing()) {
    return T::thing();
  }*/

  /*typename std::enable_if<T::has_thing, decltype(T::thing())>::type go() {
    return T::thing();
  }*/

  template <bool B=T::has_thing, typename std::enable_if<B, int>::type = 0>
  auto go() -> decltype(T::thing()) {
     return T::thing();
  }
};

int main() {
  Test<bar> b;
  Test<foo> f;
}

I can see what the problem is – decltype needs to happen before the enable_if even gets a chance to rule out the function. The question that remains is how to work around and get similar behaviour? Is there a simple, generic way to do this without resorting to writing has_thing trait to use in the enable_if?

Tested with both G++ 4.7 and clang++ 3.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-06-06T21:00:08+00:00Added an answer on June 6, 2026 at 9:00 pm

    SFINAE will work if the go method is a template method:

    template <typename T>
    struct Test {
      template <class U = T>
      auto go() -> decltype(U::thing()) {
         return T::thing();
      }
    };
    

    You could also use the has_thing value, but there’s no need to do it, since SFINAE will take care of that on the above example:

    template <class U = T, typename std::enable_if<U::has_thing, int>::type = 0>
    auto go() -> decltype(U::thing()) {
       return T::thing();
    } 
    

    Then:

    int main() {
      Test<bar> b;
      Test<foo> f;
    
      b.go(); // Works!
      f.go(); // Fails!
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It seems that using addErrorMessage() overrides all other validation errors. For example, I created
It seems that using Critical Sections quite a bit in Vista/Windows Server 2008 leads
It seems to me that using unanchored namespaces is just asking for trouble later
It seems that licensing terms would prevent us from using Google Maps API in
It seems that SQL Server does not accept numbers formatted using any particular locale.
It seems to be the prevailing opinion that using protected data members is a
I am using an iframe in my project and it seems that whenever it
I developed a site using twitter bootstrap and it seems that the responsive layout
I'm using the stackmob API in an android project and it seems that the
I am using ZUUIRevealController in my project. It seems that memory is not freed

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.