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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:30:34+00:00 2026-06-07T18:30:34+00:00

I’m new to writing template metaprogramming code (vs. just reading it). So I’m running

  • 0

I’m new to writing template metaprogramming code (vs. just reading it). So I’m running afoul of some noob issues. One of which is pretty well summarized by this non-SO post called “What happened to my SFINAE?”, which I will C++11-ize as this:

(Note: I gave the methods different names only to help with my error diagnosis in this “thought experiment” example. See @R.MartinhoFernandes’s notes on why you wouldn’t actually choose this approach in practice for non-overloads.)

#include <type_traits>

using namespace std;

template <typename T>
struct Foo {
    typename enable_if<is_pointer<T>::value, void>::type
    valid_if_pointer(T) const { }

    typename disable_if<is_pointer<T>::value, void>::type
    valid_if_not_pointer(T) const { }
};

int main(int argc, char * argv[])
{
    int someInt = 1020;
    Foo<int*>().valid_if_pointer(&someInt);    
    Foo<int>().valid_if_not_pointer(304);

    return 0;
}

@Alf says what happened to the SFINAE is “It wasn’t there in the first place”, and gives a suggestion that compiles, but templates the functions instead of the class. That might be right for some situations, but not all. (For instance: I’m specifically trying to write a container that can hold types that may or may not be copy-constructible, and I need to flip methods on and off based on that.)

As a workaround, I gave this a shot…which appears to work correctly.

#include <type_traits>

using namespace std;

template <typename T>
struct FooPointerBase {
    void valid_if_pointer(T) const { }
};

template <typename T>
struct FooNonPointerBase {
    void valid_if_not_pointer(T) const { }
};

template <typename T>
struct Foo : public conditional<
    is_pointer<T>::value, 
    FooPointerBase<T>,
    FooNonPointerBase<T> >::type {
};

int main(int argc, char * argv[])
{
    int someInt = 1020;
#if DEMONSTRATE_ERROR_CASES
    Foo<int*>().valid_if_not_pointer(&someInt);
    Foo<int>().valid_if_pointer(304);
#else
    Foo<int*>().valid_if_pointer(&someInt);
    Foo<int>().valid_if_not_pointer(304);
#endif
    return 0;
}

But if this is not broken (is it?), it’s certainly not following a good general methodology for how to turn on and off methods in a templated class based on sniffing the type for traits. Is there a better solution?

  • 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-07T18:30:37+00:00Added an answer on June 7, 2026 at 6:30 pm

    Firstly, C++11 did not carry forward boost’s disable_if. So if you’re going to transition boost code, you’ll need to use enable_if with a negated condition (or redefine your own disable_if construct).

    Secondly, for SFINAE to reach in and apply to the method level, those methods must be templates themselves. Yet your tests have to be done against those templates’ parameters…so code like enable_if<is_pointer<T> will not work. You can finesse this by making some template argument (let’s say X) default to be equal to T, and then throw in a static assertion that the caller has not explicitly specialized it to something else.

    This means that instead of writing:

    template <typename T>
    struct Foo {
        typename enable_if<is_pointer<T>::value, void>::type
        valid_if_pointer(T) const { /* ... */ }
    
        typename disable_if<is_pointer<T>::value, void>::type
        valid_if_not_pointer(T) const { /* ... */ }
    };
    

    …you would write:

    template <typename T>
    struct Foo {
        template <typename X=T>
        typename enable_if<is_pointer<X>::value, void>::type
        valid_if_pointer(T) const {
            static_assert(is_same<X,T>::value, "can't explicitly specialize");
            /* ... */
        }
    
        template <typename X=T>    
        typename enable_if<not is_pointer<X>::value, void>::type
        valid_if_not_pointer(T) const {
            static_assert(is_same<X,T>::value, "can't explicitly specialize");
            /* ... */
        }
    };
    

    Both are now templates and the enable_if uses the template parameter X, rather than T which is for the whole class. It’s specifically about the substitution that happens whilst creating the candidate set for overload resolution–in your initial version there’s no template substitution happening during the overload resolution.

    Note that the static assert is there to preserve the intent of the original problem, and prevent someone being able to compile things like:

    Foo<int>().valid_if_pointer<int*>(someInt);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I am reading a book about Javascript and jQuery and using one of the
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I would like to run a str_replace or preg_replace which looks for certain words

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.