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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:46:45+00:00 2026-06-11T16:46:45+00:00

I want to define template variables without a class, but MSVC++ does not accept

  • 0

I want to define template variables without a class, but MSVC++ does not accept it, and Googling around it seems to be incorrect according to the C++ standard:

template<CharType> static CharType hexDigits[17];
template<> char hexDigits[17] = "0123456789ABCDEF";
template<> wchar_t hexDigits[17] = L"0123456789ABCDEF";

These specialized variables will then be used from within a (non-specialized) template function.

So I’m forced to write it like this:

template<typename CharType> class dummyclass {
    static CharType hexDigits[17];
};
template<> char dummyclass<char>::hexDigits[17] = "0123456789ABCDEF";
template<> wchar_t dummyclass<wchar_t>::hexDigits[17] = L"0123456789ABCDEF";

Is there any way I can define these two variables without defining a dummy class?

Also, is there any good reason why the C++ standard does not allow the first piece of code? After all, template functions outside a class are permitted.

  • 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-11T16:46:46+00:00Added an answer on June 11, 2026 at 4:46 pm

    Also, is there any good reason why the C++ standard does not allow the first piece of code? After all, template functions outside a class are permitted.

    Note that this:

    template<CharType> static CharType hexDigits[17];
    template<> char hexDigits[17] = "0123456789ABCDEF";
    template<> wchar_t hexDigits[17] = L"0123456789ABCDEF";
    

    has two symbols with different types but the same name: this can’t possibly work, so the compiler would have to start mangling/decorating the names of variables like it already does for functions and classes.

    In terms of implementing this cleanly, this looks like a trait to me … if you don’t mind getting a link error instead of a compile error, you can even skip the specialization and only declare the appropriate static members:

    template <typename CharType> struct my_char_traits {
        static CharType hex_digits[17];
    };
    
    template<> char my_char_traits<char>::hex_digits[17] = "0123456789ABCDEF";
    template<> wchar_t my_char_traits<wchar_t>::hex_digits[17] = L"0123456789ABCDEF";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to define interface for serializing variables, where depending on a template argument,
I just want to define id attribute of body tag in child template. First
I'm trying to define a function using template template parameters (I just want to
I want to define a class that is able to populate itself reading from
I want to define different parameter according to different device, but I got a
I have a defined a simple template class. In this class I define a
I am new to Ruby, and it seems that Ruby does support variables defined
At first sorry my bad English. I want define routing for this url: For
I use emacs in ubuntu 12.04. i want define some background or foreground in
I want to define the data.store that store companyList, because I want to reuse

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.