Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.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/5/objective-c/27.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_Partial_Specialization_Class Constructors - Fatal编程技术网

C++ 如何专门化模板构造函数

C++ 如何专门化模板构造函数,c++,templates,partial,specialization,class-constructors,C++,Templates,Partial,Specialization,Class Constructors,我能很好地专业化建设者: template < typename TType > class Field { public: Field( const Msg& ) : _type( TType() ) { } protected: TType _type; }; template < > Field < double >::Field( const Msg& msg ) : _type

我能很好地专业化建设者:

template < typename TType >
class Field
{
public:
    Field( const Msg& )
        : _type( TType() )
    { }

protected:
    TType    _type;
};

template < >
Field < double >::Field( const Msg& msg )
    : _type( msg.extractDouble() )
{
}

template < >
Field < int >::Field( const Msg& msg )
    : _type( msg.extractInt() )
{
}
模板
类字段
{
公众:
字段(常量消息&)
:_类型(TType())
{ }
受保护的:
t型_型;
};
模板<>
字段::字段(常量消息和消息)
:_类型(msg.extractDouble())
{
}
模板<>
字段::字段(常量消息和消息)
:_类型(msg.extractInt())
{
}
但是,对于采用非类型参数的模板,我也需要这样做,例如:

template < const char* pszName, typename TType >
class Field
{
public:
    Field( const Msg& )
        : _type( TType() )
    { }

    static void setup( const Descriptor& d ) { // called once to setup _nIndex based on Descriptor and pszName 
    static int  index() { return _nIndex; }

protected:
    TType              _type;   // This class can only be sizeof TType in size

    static int         _index;
};

template < >
Field < ??, ?? >::Field( const Msg& msg )     // This doesn't compile
    : _type( msg.extractDouble( index() ) )
{
}

template < >
Field < ??, ?? >::Field( const Msg& msg )        // This doesn't compile
    : _type( msg.extractInt( index() ) )
{
}
template
类字段
{
公众:
字段(常量消息&)
:_类型(TType())
{ }
静态void设置(const Descriptor&d){//调用一次以基于描述符和pszName设置_nIndex
静态int索引(){return\u nIndex;}
受保护的:
TType _type;//此类的大小只能是TType的大小
静态整数指数;
};
模板<>
字段<??,??>::字段(const Msg&Msg)//这不会编译
:_类型(msg.extractDouble(index()))
{
}
模板<>
字段<??,??>::字段(const Msg&Msg)//这不会编译
:_类型(msg.extractInt(index()))
{
}

有什么诀窍可以做到这一点吗?我想我可以在运行时setup()期间传递const char name。但是如果对象本身在没有帮助的情况下知道,那就太好了。

这里的问题是,你不能部分地专门化函数,而构造函数就是一个函数。你要么完全专门化它们,要么根本不专门化它们

这个问题的一个常见解决方案是使用标记分派,但是在您的特定用例中,它稍微简单一些…使用
静态\u cast

template < typename TType, int n >
class Field
{
public:
    Field( float f )
        : _type( static_cast<TType>(f) )
    { }

protected:
    TType    _type;
};
模板
类字段
{
公众:
字段(浮动f)
:_类型(静态_类型(f))
{ }
受保护的:
t型_型;
};

这里的问题是,你不能部分地专门化函数,而构造函数就是一个函数。你要么完全专门化它们,要么根本不专门化它们

这个问题的一个常见解决方案是使用标记分派,但是在您的特定用例中,它稍微简单一些…使用
静态\u cast

template < typename TType, int n >
class Field
{
public:
    Field( float f )
        : _type( static_cast<TType>(f) )
    { }

protected:
    TType    _type;
};
模板
类字段
{
公众:
字段(浮动f)
:_类型(静态_类型(f))
{ }
受保护的:
t型_型;
};

我看不出这种局部专业化的理由(即使允许)。浮点将被隐式转换为double或int。出于我的实际需要,参数将是一个更复杂的类型。为了简单起见,我把它做成了一个POD。@edwinc:参数是否有一个到目标类型的转换运算符?我认为没有理由进行这种局部专门化(即使允许)。浮点将被隐式转换为double或int。出于我的实际需要,参数将是一个更复杂的类型。为了简单起见,我把它做成了一个POD。@edwinc:参数是否有一个到目标类型的转换运算符?谢谢你的回答。我过去对方法进行了部分专门化。你是说这不能实现吗当模板参数是值时完成?我的实际需要是这样的:@edwinc你可能部分地专门化了一个结构。方法是不可能的。谢谢你的回答。我以前做过方法的部分专门化。你是说当模板参数是值时不能这样做吗?我的实际需要是h是这样的:@edwinc您可能部分地专门化了一个结构。对于方法来说,这是不可能的。