mercredi 5 juin 2019

Fixing warning "Wundefined-var-template"

Searching for duplicates currently gives:

  • This post which specifically treats the case of a Singleton implementation, and in which the answers avoids the warning altogether with a different implementation.
  • This post which answers itself without solving the issue.
  • A suggested duplicate which explains how to implement template member functions (not relevant).
  • Another suggested duplicate explaining how to define template static members (not relevant).

As far as I understand, none of these answers the question of how to get rid of Wundefined-var-template with clang++ 3.8+ in a situation similar to the MCVE below?


File a.h

#ifndef A_INCLUDED
#define A_INCLUDED

    template <class T>
    struct A
    {
        static const char *name;
    };

#endif

File a.cpp

#include "a.h"

template <> const char* A<double>::name = "Johnny";
template <> const char* A<float>::name = "Dude";

File b.cpp

#include <cstdio>
#include "a.h"

void say_it() {
    printf( "%s\n", A<double>::name );
}

Run from a terminal:

$ clang++ -c -o a.o -std=c++11 a.cpp
$ clang++ -c -o b.o -std=c++11 b.cpp a.o
clang: warning: a.o: 'linker' input unused [-Wunused-command-line-argument]
b.cpp:5:32: warning: instantiation of variable 'A<double>::name' required here, but no definition is available [-Wundefined-var-template]
    printf( "%s\n", A<double>::name );
                               ^
./a.h:7:28: note: forward declaration of template entity is here
        static const char *name;
                           ^
b.cpp:5:32: note: add an explicit instantiation declaration to suppress this warning if 'A<double>::name' is explicitly instantiated in another translation unit
    printf( "%s\n", A<double>::name );
                               ^
1 warning generated.

Aucun commentaire:

Enregistrer un commentaire