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

  • Home
  • SEARCH
  • 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 996119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:49:42+00:00 2026-05-16T06:49:42+00:00

I have an SFINAE test for checking if an class has a function. The

  • 0

I have an SFINAE test for checking if an class has a function. The test works correctly, but I get compiler errors when I try to use it in an if statement.

//SFINAE test for setInstanceKey()
template <typename K>
class HasSetInstanceKey
{
    template <typename C>
    static char test( typeof(&C::setInstanceKey) );

    template <typename C>
    static long test(...);

public:
    enum { value = 1 == sizeof(test<K>(0)) };
};

I get “error: ‘class Node’ has no member named ‘setInstanceKey’” on the second line even though the else clause should be executing.

if ( 0 != HasSetInstanceKey<T>::value)
    instance->setInstanceKey(instanceKey);
else
    ...

Is there a way to make this work?

Thanks.

  • 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-16T06:49:43+00:00Added an answer on May 16, 2026 at 6:49 am

    Just because the if-branch is never entered doesn’t mean the code within the branch can be invalid. (Another way to think about it: you aren’t guaranteed anything about optimizations, yet your code would only be valid with a dead-branch optimization.)

    What you do is shift the branch to a function. Typically you have a framework like this:

    // holds some integral constant
    template <typename T, T V>
    struct integral_constant
    {
        static const T value = V;
    };
    
    // holds a boolean constant
    template <bool V>
    struct bool_type : integral_constant<bool, V>
    {};
    
    typedef bool_type<true> true_type; // a true boolean constant
    typedef bool_type<false> false_type; // a false boolean constant
    
    typedef const true_type& true_tag; // tag a function as the true variant
    typedef const false_type& false_tag; // tag a function as the false variant
    

    Then something like this:

    namespace detail
    {
        template <typename T, typename KeyType>
        void foo(T* instance, const KeyType& instanceKey, true_tag)
        {
            // we are in the true variant, so our meta-function's value was true
            // therefore, instance has the ability to do setInstanceKey
            instance->setInstanceKey(instanceKey);
        }
    
        template <typename T, typename KeyType>
        void foo(T*, const KeyType&, false_tag)
        {
            // we are in the false variant, so our meta-function's value was false
            // therefore, instance does not have the right capabilities, 
            // so do nothing
        }
    }
    
    // interface, forwards to correct implementation function
    template <typename T, typename KeyType>
    void foo(T* instance, const KeyType& instanceKey)
    {
        // pass instance, but to the overloaded foo 
        // that accepts the right boolean result
        detail::foo(instance, instanceKey, // plug the value into a bool_type, 
                    bool_type<HasSetInstanceKey<T>::value>()); // and instantiate it
                    // will either go into the true_tag or false_tag
    }
    

    It’s good practice to have to meta-functions inherit from the correct bool_type, to ease use:

    namespace detail
    {
        // implementation
        template <typename K>
        class HasSetInstanceKey
        {
            // note, using char and long doesn't necessarily guarantee
            // they each have a unique size. do this instead:
            typedef char yes[1];
            typedef char no[2]; // these must have different sizes
    
            template <typename C>
            static yes& test( typeof(&C::setInstanceKey) );
    
            template <typename C>
            static no& test(...);
    
        public:
            // check against size of yes result
            static const bool value = sizeof(test<K>(0)) == sizeof(yes);
        };
    }
    
    template <typename K>
    struct HasSetInstanceKey : // delegate to implementation, take result and
        bool_type<detail::HasSetInstanceKey<K>::value> // inherit from the 
                                                       // appropriate bool_type
    {};
    

    So it just becomes:

    template <typename T, typename KeyType>
    void foo(T* instance, const KeyType& instanceKey)
    {
        // because it inherits from bool_type, it can be implicitly
        // converted into either true_tag or false_tag
        detail::foo(instance, instanceKey, HasSetInstanceKey<T>());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 489k
  • Answers 489k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I can't explain why it happens, but Oracle accepts invalid… May 16, 2026 at 8:57 am
  • Editorial Team
    Editorial Team added an answer Found it, I forgot to set clipstoBounds=YES May 16, 2026 at 8:57 am
  • Editorial Team
    Editorial Team added an answer You could try a partial index: CREATE INDEX idx_partial ON… May 16, 2026 at 8:57 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I'm trying to use SFINAE to detect if a class has an overloaded member
I want to detect existence of a specific member function for a class, using
In C++0x SFINAE rules have been simplified such that any invalid expression or type
After reading the answer to this question , I learned that SFINAE can be
Have just started using Google Chrome , and noticed in parts of our site,
Have you ever seen any of there error messages? -- SQL Server 2000 Could
Have you guys had any experiences (positive or negative) by placing your source code/solution
For some complicated reason, I want to convert any supported type T (coming from
I need to designe predicate for stl algorithms such as find_if, count_if. namespace lib
I currently am adding some features to our logging-library. One of these is the

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.