Unable to declare a template variable without defining it

2k views Asked by At

I am wanting to forward declare variable templates in a header file, and then have the actual instantiations in a separate compilation unit.

I was led to believe that C++14 variable templates operated very much like static class variables do. This is unfortunately seeming not to be quite the case, and it is preventing me from forward declaring my variable templates.

template <typename T> struct Variable {
    static int variable;
};

template <typename T> 
extern int variable;

int main() {
    (void) Variable<char>::variable;
    // (void) variable<char>;                   // <-- line 10
}

template <> int Variable<char>::variable = 42;
template <> int variable<char> = 23;

The code sample above compiles and runs as-is under GCC. But uncommenting line 10 gives a compile-time error:

specialization of 'variable<char>' after instantiation
    template <> int variable<char> = 23;
                     ^
2

There are 2 answers

5
Howard Hinnant On

I think you're on the right track.

The trick is this: In any one translation unit, don't instantiate the template prior to your specialization.

For example:

// test.h
#ifndef TEST_H
#define TEST_H

template <typename T> 
extern int variable;

template <> extern int variable<char>;
template <> extern int variable<int>;

#endif // TEST_H

Then:

// test2.cpp
#include "test.h"

template <> int variable<char> = 23;
template <> int variable<int> = 24;

And finally:

// test.cpp
#include "test.h"
#include <iostream>

int
main()
{
    std::cout << variable<char> << '\n';
    std::cout << variable<int> << '\n';
}

For me this outputs:

23
24

Update

T.C. points out in the comments below that the specializations need to be declared before first use, so I've updated "test.h" above to do that.

Update 2

There seems to be some implementation divergence. clang appears to handle this fine:

template <typename T> 
extern int variable;

template <> extern int variable<char>;
template <> extern int variable<int>;

#include <iostream>

int
main()
{
    std::cout << variable<char> << '\n';
    std::cout << variable<int> << '\n';
}

template <> int variable<char> = 23;
template <> int variable<int> = 24;

http://melpon.org/wandbox/permlink/DGYKvvoPbmRIHaFi

However gcc gives an error:

prog.cc:4:13: error: explicit template specialization cannot have a storage class
 template <> extern int variable<char>;
             ^~~~~~

prog.cc:5:13: error: explicit template specialization cannot have a storage class
 template <> extern int variable<int>;
             ^~~~~~

I've searched the standard and the Core Issues list, and I can't find anything to indicate one compiler or the other is correct. If someone does see such evidence, I'm happy to include it in this answer.

0
Kaiming Yang On

I also encounter this same issue and found a solution after some research.

TL;DR: Instantiate, not specialize template. Use extern template int variable<int>, not template extern int variable<int>.

Seems extern template is a feature in C++ that make a template function an external symbol rather than compile every time. Usually this is a trick to speed up compile time but in our case it fits well.

So the whole solution would be:

variable.h

// Declare a template variable.
template <typename T> 
extern int variable;

// Implicit instantiation of variable declaration.
extern template int variable<int>;

variable.cpp

// Define a template variable.
template <typename T>
int variable = 42;

// Implicit instantiation of variable definition.
template int variable<int>;

I haven't try for other compilers but it works for clang 10.

Some reference I found for extern template.