Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/kubernetes/5.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++;使用带有枚举类和重载转换运算符的模板函数_C++_Member Functions_Function Templates_Enum Class_Conversion Operator - Fatal编程技术网

C++ c++;使用带有枚举类和重载转换运算符的模板函数

C++ c++;使用带有枚举类和重载转换运算符的模板函数,c++,member-functions,function-templates,enum-class,conversion-operator,C++,Member Functions,Function Templates,Enum Class,Conversion Operator,我正在读另一篇文章中的示例代码 我试图更进一步,通过使用重载的转换操作符来使用对象,就像它是枚举成员一样来调用模板函数 //in my .h enum class AllowedTypes { Cat, Dog }; class A { AllowedTypes animal; public: //constructor A(AllowedTypes t): animal(t) {}; explicit operator AllowedTypes*() const { return (All

我正在读另一篇文章中的示例代码

我试图更进一步,通过使用重载的转换操作符来使用对象,就像它是枚举成员一样来调用模板函数

//in my .h
enum class AllowedTypes { Cat, Dog };

class A {
AllowedTypes animal;

public:
//constructor
A(AllowedTypes t): animal(t) {};
explicit operator AllowedTypes*() const { return (AllowedTypes*) animal; }
operator AllowedTypes() const { return animal; }
template <AllowedTypes type>
void ability() const;
}

//second class
struct B{
//tempalte function
template <AllowedTypes type>
void ability() const;
}

//in my cpp
template<>
void B::ability<AllowedTypes::Dog>() const
{
std::cout << "Dog ability." << std::endl;
}
template<>
void B::ability<AllowedTypes::Cat>() const
{
std::cout << "Cat ability." << std::endl;
}


//in my main
Class* a = new A(AllowedType(1))
Class* b = new B();

//this calls work!
b->ability<AllowedTypes::Cat>(); 

//trying to get the type and calling ability via conversion doesn't
AllowedTypes type = (AllowedTypes)*a; // this converts correctly!
b->ability<type>(); //does not call correctly
//在my.h中
枚举类允许的类型{Cat,Dog};
甲级{
允许的动物类型;
公众:
//建造师
A(允许类型t):动物(t){};
显式运算符AllowedTypes*()常量{return(AllowedTypes*)animal;}
运算符AllowedTypes()常量{return animal;}
模板
无效能力()常数;
}
//二等舱
结构B{
//tempalte函数
模板
无效能力()常数;
}
//在我的cpp中
模板
void B::ability()常量
{

std::cout代码中的问题是,您试图使用运行时值而不是编译时值设置模板类型参数

Class* a = new A(AllowedType(1)) // The call to new here makes 'type' below a runtime value

b->ability<AllowedTypes::Cat>(); // AllowedTypes::Cat can be supplied at compile time

AllowedTypes type = (AllowedTypes)*a; // this converts correctly!
b->ability<type>(); //does not call correctly
Class*a=newa(AllowedType(1))//在此处调用new使“type”低于运行时值
b->指向确切问题的能力。下次当您对模板类型没有正确推断/抛出错误感到困惑时,请使用
constexpr
找出原因


下面是一个答案,解释了为什么
new
会导致格式错误的
constexpr

代码中的问题是,您试图使用运行时值而不是编译时值设置模板类型参数

Class* a = new A(AllowedType(1)) // The call to new here makes 'type' below a runtime value

b->ability<AllowedTypes::Cat>(); // AllowedTypes::Cat can be supplied at compile time

AllowedTypes type = (AllowedTypes)*a; // this converts correctly!
b->ability<type>(); //does not call correctly
Class*a=newa(AllowedType(1))//在此处调用new使“type”低于运行时值
b->指向确切问题的能力。下次当您对模板类型没有正确推断/抛出错误感到困惑时,请使用
constexpr
找出原因


下面是一个答案,解释了为什么
new
会导致格式错误的
constexpr

显式操作符AllowedTypes*()const{return(AllowedTypes*)animal;}
显式操作符AllowedTypes*()const{return AllowedTypes*)animal;}
!?