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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:37:11+00:00 2026-06-14T19:37:11+00:00

I’m experimenting a little with type traits and template specialization. For example: enum TestEnum

  • 0

I’m experimenting a little with type traits and template specialization. For example:

enum TestEnum
{
    VALUE0 = 0,
    VALUE1 = 1,
    VALUE2 = 2
    //... And so on...
};

template<int Value>
class cTestClass;

//specializations
template<>
class cTestClass<VALUE0>
{
    static int GetVal() { return VALUE0; }
};

template<>
class cTestClass<VALUE1>
{
    static int GetVal() { return VALUE1; }
};

template<>
class cTestClass<VALUE2>
{
    static int GetVal() { return VALUE2; }
};

typedef cTestClass<VALUE0> cClassVal0; //(1)
typedef cTestClass<VALUE1> cClassVal1; //(2)
typedef cTestClass<VALUE2> cClassVal2; //(3)

//Later in the code
template<int Value>
cTestClass<Value>* Create()
{
    return new cTestClass<Value>(/*..*/);
}

//And using the upper function
cClassVal2* pTestClass = Create<VALUE2>();

This works absolutely fine. The goal is to provide a way for users of an interface not to deal with templates and template parameters (Ok, besides using Create()). That is the reason for the typedefs at the bottom of the code.

No here is what I want to achieve next:
cTestClass should have one member variable which is accessible via member functions. Under certain circumstances I either want the user being able to modify the member variable or not to modify the member variable. So basically this means two member functions, one that returns the member as const reference (not modifyable) or non const (modifyable). Problem is, if both those member functions are provided, the user always has the chance to modify the member variable. So I want to choose the appropriate member function during compile time and just provide one correct member function for the user.

The only solution I came up with looks basically like this:

template<int Value, bool ConstAccess>
class cTestClass;

//specializations
template<>
class cTestClass<VALUE0, true>
{
    const std::string& get() const
    {
        return m_strName;
    }

    static int GetVal() { return VALUE0; }

private:
    std::string m_strName;
};

template<>
class cTestClass<VALUE0, false>
{
    std::string& get()
    {
        return m_strName;
    }

    static int GetVal() { return VALUE0; }

private:
    std::string m_strName;
};

//For every upcoming cTestClass two specializations

typedef cTestClass<VALUE0, true> cConstObjectVal0;
typedef cTestClass<VALUE0, false> cObjectVal0;
//And for the rest...

Besides the fact that this really seams to be unnecessary code duplication I always have to specify two typedefs for every possible VALUE. But the user should not be confronted with that choice, he should always be able to just use the types as given above (see 1, 2, 3).

Is this even possible?

I googled a bit and came up with the following site: ACCU:: An introduction to C++ traits.
This explanation seems to a matching blueprint, but I really can’t put the pieces together.
Another possibility I found is described here: C++ Template specialization to provide extra member function? Just adding one function would be possible, but this still doesn’t solve my typedef problem.

  • 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-14T19:37:13+00:00Added an answer on June 14, 2026 at 7:37 pm

    We can try to employ SFINAE:

    typedef char True;
    typedef long False;
    
    template <typename VALUE0, typename IsConst>
    class cTestClass
    {
    public:
    
        template <typename T, T> struct TypeCheck {};
    
        const std::string& get(TypeCheck<IsConst, True >* dummy = NULL);
              std::string& get(TypeCheck<IsConst, False>* dummy = NULL);
    }
    
    typedef cTestClass<VALUE0, True>  cConstObjectVal0;
    typedef cTestClass<VALUE0, False> cObjectVal0;
    

    I didn’t try to compile it yet, but the general principle is to prevent one of the functions to compile properly, then, due to the SFINAE principle, the compiler will just throw away the wrong version.

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

Sidebar

Related Questions

I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
i got an object with contents of html markup in it, for example: string
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.