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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:27:56+00:00 2026-06-02T15:27:56+00:00

Given: struct A { virtual bool what() = 0; }; template<typename T, typename Q>

  • 0

Given:

struct A
{
    virtual bool what() = 0;
};

template<typename T, typename Q>
struct B : public A
{
    virtual bool what();
};

I want to partially specialize what like:

template<typename T, typename Q>
bool B<T, Q>::what()
{
    return true;
}

template<typename Q>
bool B<float, Q>::what()
{
    return false;
}

But it appears that this isn’t possible (is it in C++11?) so I tried SFINAE:

template<typename T>
typename std::enable_if<std::is_same<T, float>::value, bool>::type B<T>::what()
{
    return true;
}

template<typename T>
typename std::enable_if<!std::is_same<T, float>::value, bool>::type B<T>::what()
{
    return false;
}

This also doesn’t work, I have no idea why though, does anyone? So I found this thread and ended up with:

template<typename T, typename Q>
struct B : public A
{
    virtual bool what()
    {
        return whatimpl(std::is_same<T, float>());
    }

    bool whatimpl(std::false_type)
    {
        return false;
    }

    bool whatimpl(std::true_type)
    {
        return true;
    }
};

This final solution works, but why doesn’t the enable_if technique work? I’m also very open to suggestions of a cleaner answer that I haven’t encountered yet.

I simplified my examples as much as possible – in my real use case what() isn’t called what and actually does a fair bit of work, and I’ll want to ‘specialize’ on a user defined type, not float.

  • 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-02T15:27:57+00:00Added an answer on June 2, 2026 at 3:27 pm

    Partial specialization is explicitly permitted by the standard only for class templates (see 14.5.5 Class template partial specializations)

    For members of class template only explicit specialization is allowed.

    14.7 (3) says:

    An explicit specialization may be declared for a function template, a class template, a member of a class
    template
    or a member template. An explicit specialization declaration is introduced by template<>.

    So any definition starting with

    template<typename T>  
    

    is not an allowed syntax for member of class template specialization.

    [edit]

    As to SFINAE attempt, it failed because actually there are neither overloads nor specializations here (SFINAE works while defining a set of candidate functions for overload resolution or while choosing proper specialization). what() is declared as a single method of class template and should have a single definition, and this definition should have a form:

    template<typename T, typename Q> 
    B<T,Q>:: bool what(){...}
    

    or may be also explicitly specialized for particular instantiation of class B:

    template<> 
    B<SomeParticularTypeT,SomeParticularTypeTypeQ>:: bool what(){...}
    

    Any other forms are syntacticaly invalid, so SFINAE can’t help.

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

Sidebar

Related Questions

Given a struct like this: public struct SomeStruct { public SomeStruct(String stringProperty, Int32 intProperty)
Given: template<class T> struct test { void foo(int b); void testFunc( int a )
With the struct definition given below... struct A { virtual void hello() = 0;
Given the following C++ code: struct vertex_type { float x, y, z; //vertex_type() {}
Given that I have something like this: int val; struct node *mhm; int val;
Given struct S { SomeType single_element_in_the_struct; }; Is it always true that sizeof(struct S)
Given two structure in c: typedef struct _X_ { int virtual_a; int virtual_b; void
Given two struct arrays A and B with field f1: A = struct('f1',{1,2,3}) B
Given this class: class C { private: struct Foo { int key1, key2, value;
Given the following code: #include <boost/noncopyable.hpp> enum Error { ERR_OK=0 }; struct Filter :

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.