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_Casting_Template Specialization - Fatal编程技术网

C++ 模板转换操作符和部分专业化

C++ 模板转换操作符和部分专业化,c++,templates,casting,template-specialization,C++,Templates,Casting,Template Specialization,我想用bool的专业化来模板化铸造操作员,但它不起作用 template<typename T> //Don't know if the fact that C is templated itself is relevant here class C { ... template<typename U> operator U() const { return another_namespace::MyCast<U>(*this); }

我想用bool的专业化来模板化铸造操作员,但它不起作用

template<typename T> //Don't know if the fact that C is templated itself is relevant here
class C
{ 
...
        template<typename U> operator U() const { return another_namespace::MyCast<U>(*this); }
        template<> operator bool() const { return IsValid(); } 
};
MyCast(它是在外部名称空间中声明的友元函数)本身也可以工作。有什么办法能让我在这里买到预定的贝霍维奥吗


编辑:我后来发现,答案看起来与基本问题相同,但答案(给出了一个非常复杂的解决方案)看起来是专门为字符串设计的。另外,这里的问题是模糊性,我认为这不是这里的问题——我得到了一个非常不同的编译器错误消息

您可以重载转换运算符,因此只需使用非模板版本的
运算符bool()
即可:

template<typename T>
class C
{ 
...
        template<typename U> operator U() const { ... }
        operator bool() const { ... } 
};
模板
C类
{ 
...
模板运算符U()常量{…}
运算符bool()常量{…}
};

您可以重载转换运算符,因此只需使用非模板版本的
运算符bool()
即可:

template<typename T>
class C
{ 
...
        template<typename U> operator U() const { ... }
        operator bool() const { ... } 
};
模板
C类
{ 
...
模板运算符U()常量{…}
运算符bool()常量{…}
};

我试图专门化类型转换操作符模板,但在类定义中定义专门化时遇到了相同的问题。但当我搬到外面时,它就像一个符咒

示例定义:

struct A {
  int i;
  A(int v) : i(v) { 
    cout << __PRETTY_FUNCTION__ << endl; 
  }
  template <typename T>
  operator T() { 
    cout << __PRETTY_FUNCTION__ << " - NOT IMPLEMENTED" << endl; 
    throw; 
    return 0;
  }

  // Compilation error
  // error: explicit specialization in non-namespace scope ‘struct A’
  //template <>
  //operator double() { 
  //  cout << __PRETTY_FUNCTION__ << " - !!! IMPLEMENTED" << endl; 
  //  return (double)i; 
  //}
};
// But this works well!!
template <>
A::operator double() { 
  cout << __PRETTY_FUNCTION__ << " - !!! IMPLEMENTED" << endl; 
  return (double)i; 
}
输出:

A::A(int)
A::operator T() [with T = double] - !!! IMPLEMENTED
A::operator T() [with T = long int] - NOT IMPLEMENTED
terminate called without an active exception
测试:g++(Debian 4.7.2-5)4.7.2,无任何额外参数


我也将在更老的g++下尝试…

我尝试专门化类型转换操作符模板,但在类定义中定义专门化时遇到了相同的问题。但当我搬到外面时,它就像一个符咒

示例定义:

struct A {
  int i;
  A(int v) : i(v) { 
    cout << __PRETTY_FUNCTION__ << endl; 
  }
  template <typename T>
  operator T() { 
    cout << __PRETTY_FUNCTION__ << " - NOT IMPLEMENTED" << endl; 
    throw; 
    return 0;
  }

  // Compilation error
  // error: explicit specialization in non-namespace scope ‘struct A’
  //template <>
  //operator double() { 
  //  cout << __PRETTY_FUNCTION__ << " - !!! IMPLEMENTED" << endl; 
  //  return (double)i; 
  //}
};
// But this works well!!
template <>
A::operator double() { 
  cout << __PRETTY_FUNCTION__ << " - !!! IMPLEMENTED" << endl; 
  return (double)i; 
}
输出:

A::A(int)
A::operator T() [with T = double] - !!! IMPLEMENTED
A::operator T() [with T = long int] - NOT IMPLEMENTED
terminate called without an active exception
测试:g++(Debian 4.7.2-5)4.7.2,无任何额外参数


我也将在更老的g++下尝试…

如果您只使用非模板化的
操作符bool()
?@sth-Ha,它不就是您想要的吗!看起来是这样。事实证明这是在一个大程序中,我还有很多其他的bug需要解决才能编译,但是编译器通过了包含类C的文件。没有意识到非专门化是一个选项。把你的回答作为答案,只要一切顺利,你就会得到奖励。
namespace::MyCast(
我认为这一部分不对。是吗?@Mooing Duck不,没关系。MyCast是在C之外声明/定义的。但实际上,由于“namespace”是一个保留字,您是对的,这可能是一个令人困惑的语法错误。因此进行了相应的编辑。如果您只使用非模板化的
运算符bool(),它不就是您想要的吗?)
?@sth-Ha!看起来是这样。原来这是在一个大程序中,我还有很多其他的bug需要解决才能编译,但是编译器通过了包含类C的文件。没有意识到非专业化是一个选项。把你的回答作为答案,你就会得到积分,只要一切顺利。
namespace::MyCast(
我认为这部分不对。是吗?@Mooing Duck不,没关系。MyCast是在C之外声明/定义的。但实际上,由于“namespace”是一个保留字,您认为这可能是一个混乱的语法错误是正确的。请相应地进行编辑。