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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:18:00+00:00 2026-06-06T19:18:00+00:00

I’ve written a simple traits class for associating string identifiers to specified types, it

  • 0

I’ve written a simple traits class for associating string identifiers to specified types, it is used like this:

typedef someclass<double,44> someD;
template<> inline const char* 
    nametrait<someD>::name() {return "some_other_double";}

then I can statically and type-safely do
std::cout << someD::name()<

and I get some_other_double. This is to be used to wrap an old
database-like structure with integer keys to a more typesafe
treatment, where each field is represented by their own type, and the
some_other_double represents the field name in the database.

This works fine, and I also get a reasonable compilation error when no
name is set (find my full code at the end). However, I wonder if there’s
some better pattern for achieving the same effect. I’ll define many
object like these, and what I’m really after is something more like

typedef someclass<double,44,"some_other_double"> someD;

but litteral string constants are not permitted to be used like this (not even in c++11 I think). If
I was alone working on this code I would probably use a macro, but I’m
not that keen on defending it

#define namedtype(ss,tt,id,nn,newname)\
typedef ss<tt,id> newname;  \
template<> inline const char* nametrait<newname>::name() {return nn;}

I also experimented with specifying static name constants, along the lines of:
template<>
const char * someclass<double,44>::name = "some_other_double";

but I think it’s not possible to make sure they are defined once and
only once without even more text than the above (I need to force
template instantiation for each declared one in some cpp file).
I prefer compile time errors before link errors.

Here’s my code:

// file: mytypes.h (I omit the include guard)

// this is the default which generates an error message on looking up
// MISSING_DEFINITION_OF_name
template <typename T>
class nametrait
{
public:
   static inline const char* name() {return T::MISSING_DEFINITION_OF_name();};
};

template <typename T,int identifier>
class someclass {
private:
   someclass(){};
public:
   static const char * name() {return nametrait<someclass>::name() ;};
};

typedef someclass<double,44> someD;
template<> inline const char* nametrait<someD>::name() {return "some other double";}

typedef someclass<int,55> someI;
template<> inline const char* nametrait<someI>::name() {return "some specific int";}

typedef someclass<char,22> someC;
template<> inline const char* nametrait<someC>::name() {return "some specific char";}

typedef someclass<char,5> someC;
template<> inline const char* nametrait<someC>::name() {return "some other char";}

use:

// file:: main.cpp
#include mytypes.h
int main(){
   std::cout << someI::name()<<std::endl;
   std::cout << someD::name()<<std::endl;   
   std::cout << someC::name()<<std::endl;
   std::cout << someC2::name()<<std::endl;
}

Questions:

  1. Does anyone have a better solution?
  2. Is my understanding correct that there’s normally not going to be any extra cost of the inline template method returning a string compared to static string constants? (for example using gcc with -g -O, which indeed does not imply -fno-inline).
  • 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-06T19:18:01+00:00Added an answer on June 6, 2026 at 7:18 pm

    I’m not sure if it is better, but this is slightly shorter:

    template <typename T,int identifier>
    class someclass {
    private:
       someclass(){};
    public:
       static inline const char * name();
    };
    
    
    typedef someclass<double,44> someD;
    
    template <> inline const char * someD::name() {return "some other double"; }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I would like to count the length of a string with PHP. The string
I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
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.