有两种方法可以显式实例化CRTP:
1:
//base.h-------------------------------------
template <typename T>
class Base
{
public:
static void func();
};
//base.cpp--------------------------------
#include "Foo.h"
template<typename T>
void Base<T>::func()
{
}
//derive.h----------------------------------------
#include "base.h"
class Derive : public Base<Derive>
{
public:
};
template class Base<Derive>; //force explicit instantiation <----
在Visual Studio 2017中获得一些警告:
警告C4661:'void Base :: func(void)':不适用 为显式模板实例化请求提供的定义
2:
//base.h------------------------------------
class Derive;
template <typename T>
class Base
{
public:
static void func();
};
template class Base<Derive>; //force explicit instantiation <------
//derive.h-------------------------------------
#include "base.h"
class Derive : public Base<Derive>
{
public:
};
获得相同的警告:
警告C4661:'void Base :: func(void)':不适用 为显式模板实例化请求提供的定义
哪个是正确的?