I know. The headline is not perfekt. Perhaps I fund a better Headline later. I have a Problem with GTEST with template. (Yes I know. The code make no sense. It is a sample). I have the following template:
template<typename T>
struct MY_VALUE{};
template<>
struct MY_VALUE<uint8_t>{
static const uint8_t val = 8;
};
template<>
struct MY_VALUE<uint16_t>{
static const uint16_t val = 16;
};
I can test the code with (test sucess):
TEST(MY_VALUE_test, test) {
uint32_t var8 = MY_VALUE<uint8_t>::val;
ASSERT_EQ(8, var8);
uint32_t var16 = MY_VALUE<uint16_t>::val;
ASSERT_EQ(16, var16);
}
But when i try to test this, the LINKER give me an error:
TEST(MY_VALUE_test, test1) {
ASSERT_EQ(8, MY_VALUE<uint8_t>::val);
ASSERT_EQ(16, MY_VALUE<uint16_t>::val);
}
Linker error:
undefined reference to `MY_VALUE<unsigned char>::val
undefined reference to `MY_VALUE<unsigned short>::val
anyone an idea :) Thanks
The problem is with the fact that assertion engine of GTEST requires reference. To have reference - variable needs to be defined:
template<>
struct MY_VALUE<uint8_t>{
static const uint8_t val = 8;
};
// this line is missing
// Note: do not put it in header - if you have multiple files project
const uint8_t MY_VALUE<uint8_t>::val;
Do the same for uint16_t.
IF you have compiler supporting C++17 - you might try to just add inline or constexpr to val:
template<>
struct MY_VALUE<uint8_t>{
static constexpr uint8_t val = 8;
};