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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T12:55:58+00:00 2026-06-16T12:55:58+00:00

I was wondering if it was possible to create a class that would serve

  • 0

I was wondering if it was possible to create a class that would serve as a combination between std::enable_if and a SFINAE member detector.

class foo
{
public:
    int bar;
};

template <class T>
typename enable_if_has_bar<T>::type ReturnBar (const T& value)
{
    return value.bar;
}

So I attempted to do this.

class foo
{
public:
    int bar;
};

template <class C, C>
class Check;

template <class T, class Enable = void>
class enable_if_has_bar
{};

template <class T>
class enable_if_has_bar<T, Check <decltype(&T::bar),&T::bar>>
{
public:
    typedef decltype(static_cast<T*>(0)->*static_cast<decltype(&T::bar)>(0)) type;
};

template <class T>
typename enable_if_has_bar<T>::type ReturnBar (const T& value)
{
    return value.bar;
}

int main ()
{
    foo foobar;
    foobar.bar = 42;

    cout << ReturnBar(foobar) << endl;
}

( http://ideone.com/WKTfmQ )

It doesn’t seem to work, and I’m not quite as versed in the fine art of SFINAE as I could be. Perhaps someone could improve/fix it? Because I’m at a loss.

  • 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-16T12:55:59+00:00Added an answer on June 16, 2026 at 12:55 pm

    I generally prefer to create custom enable_if-style types as you’ve attempted, because I find the code clearer to read with a single trait type, rather than a combination of enable_if<some_trait<T>, another_trait<T>>. But in this case there are a few problems in your code stopping it working.

    Your enable_if_has_bar specialization will never be selected, the return type of ReturnBar just instantiates the primary template, enable_if_has_bar<foo, void>, and that never defines the nested type. Nothing causes the specialization to be instantiated so there is nothing that checks whether T::bar is a valid expression.

    Your decltype(static_cast<T*>(0)->*static_cast<decltype(&T::bar)>(0)) expression will result in int& not int as you probably want. This is because decltype(foobar.*(&foo::bar)) is equivalent to decltype(foobar.bar) and foobar.bar is an lvalue, so the decltype is int&. The function ReturnBar would fail to compile if it returns int& because the parameter value is const, so you can’t bind value.bar to a non-const int&.

    Here’s a working version:

    template <class T>
      class has_bar
      {
        template<typename U, typename = decltype(&U::bar)>
          static std::true_type
          test(U*);
    
        static std::false_type
        test(...);
    
      public:
        static const int value = decltype(test((T*)nullptr))::value;
      };
    
    template<typename T, bool = has_bar<T>::value>
      struct enable_if_has_bar
      { };
    
    template<typename T>
      struct enable_if_has_bar<T, true>
      : std::decay<decltype(std::declval<T&>().*(&T::bar))>
      { };
    

    This first declares the helper has_bar to answer the question of whether the type has the nested member. That helper uses SFINAE to get a true or false value. If &T::bar is a valid expression then the first overload of test will be used, which returns true_type and so value will be set to true_type::value i.e. true. Otherwise the fallback overload will be selected and value set to false.

    Then the enable_if_has_bar template uses a default template parameter which is deduced as the value of has_bar<T>::value. The primary template is used when has_bar<T>::value is false. The specialization is used when has_bar<T>::value is true, in which case we know the expression &T::bar is valid and can use it in a decltype expression to get the type.

    std::decay is used to turn the int& result of the decltype expression to just int. Inheriting from decay is a bit shorter than using it to define the member, which would be:

    typedef typename std::decay<decltype(std::declval<T&>().*(&T::bar))>::type type;
    

    I used the standard utility declval<T>() in the unevaluated expressions, which is a bit shorter to type and a bit more idiomatic and expressive than static_cast<T*>(0).

    An alternative to using decay would be another helper type to get the type int from the type int T::* e.g.

    template<typename T>
      struct remove_class;
      { };
    
    template<typename Member, typename Class>
      struct remove_class<Member Class::*>
      {
        typedef Member type;
      };
    
    template<typename T>
      struct enable_if_has_bar<T, true>
      : remove_class<decltype(&T::bar)>
      { };
    

    (The name remove_class isn’t very good, but basically it takes a pointer-to-data-member type and gives the type of the member.)

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

Sidebar

Related Questions

I was wondering if the following is possible. Create a class that accepts an
I was wondering if it is possible to create a flash movie that rotates
I was wondering how it would be possible to create my own listener for
I was wondering if it was possible to create a double rounded border without
I was wondering if it was possible to create a SPARQL UpdateRequest in Jena
I am wondering if it is possible to create a new dataframe with certain
Hey I was just wondering if it is possible to create an executable file
Just wondering if it is possible to dynamically create a list of strings in
I was wondering if it were possible to dynamically create an XML layout file
I was wondering ( if possible ) if there was a program/tool/utility that when

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.