Here is my working code example:
#include <iostream>
template<typename B>
class b {
public:
int y;
constexpr b(int x) : y(x) {
}
constexpr void sayhi() {
std::cout << "hi" << std::endl;
}
};
template<int x>
struct A {
static constexpr b<int> bee = x;
static constexpr int y = x; // this one is fine and usable already, I don't have to do something like what I did on member bee
inline static void sayhi() {
std::cout << y << std::endl;
}
};
template<int x>
constexpr b<int> A<x>::bee; // why do I have to do something like this, it will cause errors if I don't.
int main(int argc, char** argv) {
A<30>::bee.sayhi(); // works fine
A<30>::sayhi(); // works fine
return 0;
}
What my code does is simple, I have template struct A
that has two static variables, namely a static constexpr int y
and a static constexpr b<int> bee = x;
. My template struct A
will get the value of the argument which will be copied by x
from the template parameter. My question is: how come when it comes to classes, I have to initialize the class by doing something like this:
template<int x>
constexpr b<int> A<x>::bee;
If I don't use the code above, I get the undefined
reference error. Wherein the int is already fine and accessible just from doing something like:
static constexpr int y = x;
I am concerned why I don't have to forward declare it anymore.