mercredi 17 août 2016

Variadic template function won't link [duplicate]

This question already has an answer here:

cmds.cpp has this:

namespace TestNamespace
{
    template<typename... Args>
    void tprint(Args const& ... args)
    {
        std::ostringstream message;
        using List= int[];
        (void)List{0, ((void)(message << args), 0) ... };

        std::cout << message.str();
    }
}

cmds.h has this:

namespace TestNamespace
{
    template<typename... Args> void tprint(Args const& ... args);
}

main.cpp has this:

#include "cmds.h"

using namespace TestNamespace;

void func()
{
    tprint("test\n");
}

Compiler happily compiles. Linker, however, says:

main.cpp: undefined reference to `void TestNamespace::tprint<char [6]>(char const (&) [6])'
collect2: error: ld returned 1 exit status

Makefile flags:

CPPFLAGS=-std=c++11 -m32 -Wall -g -I./ -Wno-trigraphs
LDFLAGS=-std=c++11 -m32 -L./ -lm

What is going on?

Aucun commentaire:

Enregistrer un commentaire