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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:48:06+00:00 2026-06-01T17:48:06+00:00

As part of a traits class for a larger program, I attempted to create

  • 0

As part of a traits class for a larger program, I attempted to create a static class variable which could have a different value depending on the type that the enclosing class template was instantiated with.

I’ve simplified the relevant code to produce a bare-bones example of what I’m talking about:

#include <iostream>
#include <string>
#include <type_traits>

template <class T, class Enable = void>
struct Foo;

template <class T>
struct Foo<T,
    typename std::enable_if<std::is_integral<T>::value>::type
>
{
    static std::string message;
};

template <class T>
struct Foo<T,
    typename std::enable_if<std::is_floating_point<T>::value>::type
>
{
    static std::string message;
};

template <class T, class Enable>
std::string Foo<T, Enable>::message;

With GCC 4.6, this gives a compiler error: template definition of non-template ‘std::string Foo<T, Enable>::message. The problem occurs because of the last two lines, where I’m just defining the static variable std::string Foo<T, Enable>::message.

I’m confused why this is happening. The compiler error goes away if I omit the last two lines (but of course that causes linker errors.) Is this a compiler error with GCC?

  • 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-01T17:48:08+00:00Added an answer on June 1, 2026 at 5:48 pm

    This only works when your template parameters match the partial specialization:

    template <class T>
    std::string Foo<T,
        typename std::enable_if<std::is_integral<T>::value>::type
    >::message;
    
    template <class T>
    std::string Foo<T,
        typename std::enable_if<std::is_floating_point<T>::value>::type
    >::message;
    

    This is specified in section 14.5.4.3 of the C++03 standard. The idea is that a partial specialization is a new template and the template parameters of members that are defined externally have to match the template parameters of the class definition so that it knows which template the member goes with.

    In your example, the rule avoids defining the message member for types that are not integral or floating point.

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

Sidebar

Related Questions

Part of a larger program I am making requires a path to be read
Part of a new product I have been assigned to work on involves server-side
I try to write a wrapper class for leveldb. Basically the part of the
Why if I have: trait T { def method(a: Int) } class A extends
I have a small project where I need just part of boost library, boost::regex
Quick note: Examples from the tutorial Scala for Java Refugees Part 5: Traits and
part of my gitignore file looks like this: phoenix/config/ProjectConfiguration.class.php StatsFile.txt phoenix/StatsFile.txt phoenix/config/* phoenix/config/propel.ini phoenix/data/*
Part of my program requires me to be able to randomly shuffle list elements.
Part of my program is to calculate sqrt of float number. When I write
I have the following situation (simplified): a.h: #include <boost/serialisation/serialisation.hpp> class B; using namespace std;

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.