【发布时间】:2018-10-25 14:53:12
【问题描述】:
这不打印:
#include <iostream>
template <typename Derived>
struct A
{
static int test()
{
std::cout << "test" << std::endl;
return 0;
}
static inline int a = test();
};
struct B : public A<B>
{
};
int main(int argc, char** argv)
{
return EXIT_SUCCESS;
}
但这确实:
#include <iostream>
template <typename Derived>
struct A
{
};
struct B : public A<B>
{
static int test()
{
std::cout << "test" << std::endl;
return 0;
}
static inline int a = test();
};
int main(int argc, char** argv)
{
return EXIT_SUCCESS;
}
还有这个:
#include <iostream>
struct A
{
static int test()
{
std::cout << "test" << std::endl;
return 0;
}
static inline int a = test();
};
struct B : public A
{
};
int main(int argc, char** argv)
{
return EXIT_SUCCESS;
}
不确定原因或解决方法。我需要“派生”类型将其注册到静态表中。
【问题讨论】:
-
第二个有什么问题?
-
@user463035818 想必你必须在每个派生类中编写它。
-
@MaxLanghof 如果
test放在(非模板化)基类中,则不会
标签: c++