below is my code:
// types.h
template <typename T>
struct type_to_char {};
template <>
struct type_to_char<char> {
static constexpr char str[] = "baz";
};
// main.cpp
#include <iostream>
#include <string>
#include "types.h"
int main() {
std::cout << type_to_char<char>::str << std::endl;
return 0;
}
On an attempt to compile, the linker returns an error:
undefined reference to type_to_char<char>::str
I have encountered this answer, but I am not sure how to apply it in my case, since templates are not compiled. Should I put a seperate .cpp
file in the project?
What is the difference between declaration and definition for constexpr
variables? Such variable cannot be declared without an initializer, so why should I put a separate definition in a .cpp
file?
I would appreciate some clarification on this