Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.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++项目,它具有以下类模板。注意,最后一个参数应该是可选的,如果没有设置值,则默认为0 我知道,在C++ 11之前,C++的标准是非法的,我使用的GCC版本也不能升级。这是嵌入式软件环境,工具链在一定程度上由外部公司维护 template< typename TVal, typename Tobj, TVal (Tobj::*TGet)(), bool (Tobj::*TSet)(TVal) = 0 > class TPropertyEx { typedef bool (Tobj::*pSetter)(TVal); typedef TVal (Tobj::*pGetter)(); Tobj *pObj; pGetter Get; pSetter Set; explicit TPropertyEx(Tobj *pobj):mReadOnly(false),pObj(pobj),Get(TGet),Set(TSet){} explicit TPropertyEx(Tobj *pobj, const TVal &value):mReadOnly(false),pObj(pobj),Get(TGet),Set(TSet) // rest omitted for brevity_C++_Templates_Arguments - Fatal编程技术网

需要空值时的默认模板参数 我正在研究一个C++项目,它具有以下类模板。注意,最后一个参数应该是可选的,如果没有设置值,则默认为0 我知道,在C++ 11之前,C++的标准是非法的,我使用的GCC版本也不能升级。这是嵌入式软件环境,工具链在一定程度上由外部公司维护 template< typename TVal, typename Tobj, TVal (Tobj::*TGet)(), bool (Tobj::*TSet)(TVal) = 0 > class TPropertyEx { typedef bool (Tobj::*pSetter)(TVal); typedef TVal (Tobj::*pGetter)(); Tobj *pObj; pGetter Get; pSetter Set; explicit TPropertyEx(Tobj *pobj):mReadOnly(false),pObj(pobj),Get(TGet),Set(TSet){} explicit TPropertyEx(Tobj *pobj, const TVal &value):mReadOnly(false),pObj(pobj),Get(TGet),Set(TSet) // rest omitted for brevity

需要空值时的默认模板参数 我正在研究一个C++项目,它具有以下类模板。注意,最后一个参数应该是可选的,如果没有设置值,则默认为0 我知道,在C++ 11之前,C++的标准是非法的,我使用的GCC版本也不能升级。这是嵌入式软件环境,工具链在一定程度上由外部公司维护 template< typename TVal, typename Tobj, TVal (Tobj::*TGet)(), bool (Tobj::*TSet)(TVal) = 0 > class TPropertyEx { typedef bool (Tobj::*pSetter)(TVal); typedef TVal (Tobj::*pGetter)(); Tobj *pObj; pGetter Get; pSetter Set; explicit TPropertyEx(Tobj *pobj):mReadOnly(false),pObj(pobj),Get(TGet),Set(TSet){} explicit TPropertyEx(Tobj *pobj, const TVal &value):mReadOnly(false),pObj(pobj),Get(TGet),Set(TSet) // rest omitted for brevity,c++,templates,arguments,C++,Templates,Arguments,请注意,计数“属性”应该是只读属性;你永远不会把它设定为任何东西 目前(不允许传递null模板参数),我必须这样编写上述代码: TPropertyEx<unsigned int, SomeClass, &SomeClass::iCount, &SomeClass::iCountRO> Count; 这会产生错误: error: could not convert template argument 'ReadOnlySet' to .... 我试过这个: te

请注意,
计数
“属性”应该是只读属性;你永远不会把它设定为任何东西

目前(不允许传递null模板参数),我必须这样编写上述代码:

  TPropertyEx<unsigned int, SomeClass, &SomeClass::iCount, &SomeClass::iCountRO> Count;
这会产生错误:

error: could not convert template argument 'ReadOnlySet' to ....
我试过这个:

template< typename TVal, typename Tobj, TVal (Tobj::*TGet)(), bool (Tobj::*TSet)(TVal) = &TPropertyEx::ReadOnlySet >
class TPropertyEx
{
private:

    bool ReadOnlySet(TVal val){return false;}
如果没有设置值,我如何将
TSet
设置为某个默认函数,或者我必须始终指定
TSet

另一个我有但还没有研究过的想法是对
TPropertyEx
进行模板专门化,而
TSet
值不在模板定义中


建议?

您可以通过类似“策略”类的方式间接传递所有模板参数。要定义这个类,您可以提供两个不同的类模板(一个带最后一个模板参数,一个不带最后一个模板参数)。您尝试的通常是使用traits specifier来完成的,这基本上就是@dyp所描述的。一个微不足道的例子
error: could not convert template argument 'ReadOnlySet' to ....
template< typename TVal, typename Tobj, TVal (Tobj::*TGet)(), bool (Tobj::*TSet)(TVal) = &TPropertyEx::ReadOnlySet >
class TPropertyEx
{
private:

    bool ReadOnlySet(TVal val){return false;}
error: 'TPropertyEx' has not been declared; which is dubious as TPropertyEx would not be TObj:: so I get why the compiler complains on this too.