Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 静态字段初始化的模板部分专门化_C++_Templates_Static Members_Template Specialization - Fatal编程技术网

C++ 静态字段初始化的模板部分专门化

C++ 静态字段初始化的模板部分专门化,c++,templates,static-members,template-specialization,C++,Templates,Static Members,Template Specialization,我正在尝试以下方法: struct MyType { }; template <typename T> struct Test { static const MyType * const sm_object; }; template <> struct Test<void> { static const MyType * const sm_object; }; template <typename T> const MyTyp

我正在尝试以下方法:

struct MyType { };

template <typename T>
struct Test
{
    static const MyType * const sm_object;
};

template <>
struct Test<void>
{
    static const MyType * const sm_object;
};

template <typename T> const MyType * const Test<T>::sm_object = new MyType();
template <> const MyType * const Test<void>::sm_object = new MyType();
struct MyType{};
模板
结构测试
{
静态常量MyType*常量sm_对象;
};
模板
结构测试
{
静态常量MyType*常量sm_对象;
};
模板常量MyType*常量测试::sm_对象=新的MyType();
模板常量MyType*常量测试::sm_对象=新的MyType();
我将其包含在两个文件中-a.cpp和b.cpp。我试图编译并获取:

error C2998: 'const MyType *Test<void>::sm_object' : cannot be a template definition
错误C2998:'const MyType*Test::sm_object':不能是模板定义
我假设我的C++语法不好,但我不能想象我做错了什么。 我无法从变量定义中删除
模板
,因为我需要在多个翻译单元中使用该模板,这将导致链接错误

我可以将字段放入基类中,并使用CRTP为每种类型创建一个新实例,这样专门化就不会妨碍,但为什么这个“直接”字段初始化不起作用呢?我一定是漏掉了一些语法


我使用的是VS2003:(

从g++判断,我认为您需要从该行中删除
模板
,并将剩余部分放在一个源文件中(而不是放在头文件中)。由于它是一个专门化,它就像一个普通的非模板静态文件,您不需要在头文件中定义

在一些
.C
文件中:


const MyType*const Test::sm_object=new MyType();

我相信以下代码可能会引起一些人的兴趣:

#include <stdio.h>
template<class X,int Y>
struct B
{
  X content;
  static const int nr=Y;
};

int main(int, char**)
{
  B<char,1> a;
  B<int,2> b;
  B<int,3> c;
  printf("%d, %d, %d\n",a.nr,b.nr,c.nr);
}
#包括
模板
结构B
{
X含量;
静态常数int nr=Y;
};
int main(int,char**)
{
B a;
B B;
B c;
printf(“%d,%d,%d\n”,a.nr,b.nr,c.nr);
}

我相信你想做这样的事情

struct MyType { };

template <typename T>
struct Test
{
    static const MyType * const sm_object;
    static const MyType* set_object()
    {
        return nullptr;
    }
};

template <>
struct Test<void>
{
    static const MyType * const sm_object;
    static const MyType* set_object()
    {
        return new MyType();
    }
};

template <typename T> 
const MyType * Test<T>::sm_object = Test< T >::set_object();
struct MyType{};
模板
结构测试
{
静态常量MyType*常量sm_对象;
静态常量MyType*集合_对象()
{
返回空ptr;
}
};
模板
结构测试
{
静态常量MyType*常量sm_对象;
静态常量MyType*集合_对象()
{
返回新的MyType();
}
};
模板
常量MyType*Test::sm_object=Test::set_object();

我希望这是一个只有标题的组件,而不是有一个静态库来链接,所以我使用CRTP来实现它来保存静态对象。将答案标记为“你是对的”,这是关于模板专门化的链接属性。答案是正确的,但我最终使用了一个新的struct TestHolder,其中olds sm_对象,然后holder类只需要一个静态初始化,而不是部分专业化的初始化。是的,这很有趣,但最初的问题更多的是关于在显式专业化的链接阶段静态成员的行为。