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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:31:30+00:00 2026-05-23T22:31:30+00:00

So I got a class that specifies a constant with a special meaning for

  • 0

So I got a class that specifies a constant with a special meaning for each type.
I use something like this

template<class Type>
class SpecialKeyProvider
{
  static const Type SPECIAL_KEY;
}

with one or more specializations for Types that will also be used but are not numeric.
So I initialize the static member outside the class’ definition like I have to:

// This seems to have to be in a cpp as having it in the same header, 
// will result in duplicate definitions of SPECIAL_KEY;

// This particular instantiation is kind of problematic anyways, see
// the end of my question for another question on this one.
template<>
string SpecialKeyProvider<string>::SPECIAL_KEY = "__SPECIAL";

template <class Type>
Type SpecialKeyProvider<Type> = std::numeric_limits<Type>::max();

Unfortunately the SpecialKeyProvider is used in a very central Container class. This container has lots of methods that should be inlined for performance reasons and is used in numerous places. So I put the whole container in the header file. But now that the SpecialKeyProvider is used, I still have to link SpecialKeyProvider.o to pretty much every single binary in my project.

Is there any way to get rid of that and to do the template specialization in the header without getting an error due to “multiple definitions”?

Second question concerning the string constant.

// I know this is bad code but I don't know how to do it better.
template<>
string SpecialKeyProvider<string>::SPECIAL_KEY = "__SPECIAL";

I know (and the linter reminds me), that using constants of any class type is generally a bad idea due to indeterminant order of construction (google style guide), in particular strings. While I can normally use a const char[] instead, I don’t know what to do in this case, since I want the template to be instantiated for strings. Using the template (i.e. using my container class that used the SpecialKeyProvider) with char* instead would be incredibly painful.

I’m really thankful for any advice.

  • 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-23T22:31:31+00:00Added an answer on May 23, 2026 at 10:31 pm

    Put the class with static const in an unnamed namespace in your header file:

    namespace
    {
      template<class Type>
      class SpecialKeyProvider
      {
        static const Type SPECIAL_KEY;
      };
      template<>
      const string SpecialKeyProvider<string>::SPECIAL_KEY = "__SPECIAL";  //<--- const
    
      template<>
      const double SpecialKeyProvider<double>::SPECIAL_KEY = 3.3;  //<--- const
    }
    

    This will not cause any linker error even if you declare variables in a header file; Because for every .cpp file a different unnamed namespace is created. For your case you have only const variables declared, so there is an assurity that all the .cpp files are reading the same value.

    Edit: Regarding your second question, I don’t know what could be the problem with the above approach as for every translation unit a different const copy will be created. See if the lint still complains.

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

Sidebar

Related Questions

I've got a class that looks something like this: public class Parent { public
I've got a simple java class that looks something like this: public class Skin
I'm sure this must be a common problem. I've got a class that in
I've got a function inside of a class that returns a string. Inside this
I've got a nice little class built that acts as a cache. Each item
Got a class that serializes into xml with XMLEncoder nicely with all the variables
I've got a class that I'm using as a settings class that is serialized
I've got a simple class that inherits from Collection and adds a couple of
I've got a class named BackgroundWorker that has a thread constantly running. To turn
I've got a (poorly written) base class that I want to wrap in a

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.