I have some doubts about what the standard says about the following case:
//foo.hpp
#pragma once
#include<iostream>
template<typename T>
void foo(){
std::cout << "generic\n";
}
#ifdef FWD
template<>
void foo<int>();
#endif
//foo.cpp
#include "foo.hpp"
template<>
void foo<int>(){
std::cout << "specialization\n";
}
\\main.cpp
#include "foo.hpp"
int main(){
foo<int>();
}
The result of this code is the same no matter the definition of FWD (print "specialization"). However, if I check symbols in main.o, results are different:
- FWD defined : undefined symbol to specialization of foo
- FWD undefined : the symbol to foo exists
Is there a standard way of dealing with this? Is there any risk that the general implementation is taken at link-time when no forward declaration is done?