1

Why isn't this code working and giving an "undefined max" ?

#include <iostream>
using namespace std;

template<typename T>
struct Foo {
  static T const max;
};

template<> struct Foo<int> { // Specialization
    static int max; 
};

template<typename T> T const Foo<T>::max = 22;

template struct Foo<int>;

int main() {

    struct Foo<int> ma;
    cout << ma.max;

    return 0;
}

I defined the static variable and I instantiated the template (I believe the explicit instantiation is useless here).

What's wrong?

user129506
  • 337
  • 2
  • 10
  • check out this http://stackoverflow.com/questions/2342550/static-member-initialization-for-specialized-template-class – Rakib Jun 20 '14 at 09:19

1 Answers1

1

template<typename T> T const Foo<T>::max = 22; is the definition of the general case, not for the specialization.

You have also to define int Foo<int>::max = 22; for the int specialization.

Jarod42
  • 203,559
  • 14
  • 181
  • 302