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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:33:09+00:00 2026-06-03T22:33:09+00:00

In the code below, Foo<T>::setValue works well for my purposes, except in cases where

  • 0

In the code below, Foo<T>::setValue works well for my purposes, except in cases where where T is a class enum named TYPE e.g. Bar::TYPE and Baz:TYPE.

Therefore, I’d appreciate help specializing Foo<T>::setValue without naming Bar and Baz, because there could be dozens of such classes.

class Bar
{
public:
    enum TYPE{ ONE , TWO };
};

class Baz
{
public:
    enum TYPE{ SIX , TEN };
};

template<typename T>
class Foo
{
public:
    void setValue(){} // Need a different setValue if T is a class enum

private:
    T m_value;
};


int main()
{
    Foo<int> f1;
    Foo<Bar::TYPE> f2;
    Foo<Baz::TYPE> f3; 
    return EXIT_SUCCESS;
}
  • 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-03T22:33:11+00:00Added an answer on June 3, 2026 at 10:33 pm

    You can do this with something like:

    #include <type_traits>
    
    template<typename T>
    class Foo
    {
    public:
        void setValue() {
          setValueImpl<T>();
        }
    private:
        template <class X>
        typename std::enable_if<std::is_enum<X>::value, void>::type
        setValueImpl() { std::cout << "Is enum" << std::endl; }
    
        template <class X>
        typename std::enable_if<!std::is_enum<X>::value, void>::type
        setValueImpl() { std::cout << "Not enum" << std::endl; } 
    
        T m_value;
    };
    

    Where enable_if picks which version to use based on the is_enum type trait.

    The example used C++11 enable_if and is_enum but boost has similar for pre-C++11 also.

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

Sidebar

Related Questions

In the code below, I've defined two overloaded methods named Bar . In Foo()
Code below is working well as long as I have class ClassSameAssembly in same
For the C++ code fragment below: class Foo { int a[]; // no error
In the code below foo returns a type A object by value. Can I
Given the code below the method foo should compare operator-wise a given parameter bar
The code below has me slightly perplexed: function(__m128 foo) { __m128 bar = _mm_shuffle_ps(foo,
In the sample code below, the method test() in parent class Foo is overridden
Both of these code blocks below alert foo then bar . The only difference
can anyone explain why the code below fails? type TIDEThemeObserverFunc = reference to procedure(foo:
The code below (for running in LinqPad) is meant to parse the foo/skip/bar string

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.