如何为类编写“获取”方法模板

时间:2019-12-26 10:41:58

标签: c++ templates

传统上,如果我想获取一个类的私有属性,则必须只为其声明一个get方法。现在,我想要一个get方法,该方法将返回其类中的任何属性,以防该类有许多属性要获取。 我想要的是这个

function get_prop(attr_index)
input: the property's index inside class declaration
output: that property's value as constant.

我尝试过:

#include<iostream>
#include<string>
class myClass{
private:
long age;
std::string name;
public:
myClass(long = 0, string = "");
template<typename T>
const T& get_prop(int) const;      //line 10
};
myClass::myClass(long _age, std::string _name): age(_age), name(_name){}
template<typename T>
const T& myClass::get_prop(int prop_index) const {
switch(prop_index){
case 1: return age;
case 2: return name;
}
}
int main(){
myClass ob1(10,"someone");
std::cout<<ob1.get_prop(1);        //line 22
return 0;
}

但是编译器给出了错误: build message
如果我添加一个参数来指定返回类型,例如:

class myClass{
...
template<typename T>
const T& get_prop(int, T) const;
...
};
template<typename T>
const T& myClass::get_prop(int prop_index, T) const {
switch(prop_index){
case 1: return age;           //line 16
case 2: return name;          //line 17
}
}
int main(){
myClass ob1(10,"someone");
std::cout<<ob1.get_prop(1,int());//line 22
return 0;
}

编译器给出此错误: new build message
有人可以告诉我如何编码吗?

2 个答案:

答案 0 :(得分:3)

例如:

template<std::size_t prop_index>
auto& myClass::get_prop() const {
    if constexpr (prop_index == 1)
        return age;
    else if constexpr (prop_index == 2)
        return name;
}

int main() {
    myClass ob1(10, "someone");
    std::cout << ob1.get_prop<1>();
    return 0;
}

此代码需要C ++ 17编译器。

  

switch可以工作吗?

在一般情况下,不会。与constexpr不同,它不是if constexprget_prop的返回类型将从所有return语句推导出。如果所有推导的类型都是相同的类型(例如long),它将起作用。如果它们是不同的(例如longstd::string),则it won't

  

如果有多个return语句,则它们都必须推导为同一类型。

出于同样的原因,普通if也将不起作用。相反,if constexpr的未使用分支在编译时被丢弃,并且该分支中的return语句不参与返回类型推导。

答案 1 :(得分:3)

主要问题是必须在编译时知道prop_index。您可以将其设为模板参数并应用constexpr if(自C ++ 17起)。

例如

template<int prop_index>
const auto& myClass::get_prop() const {
if constexpr (prop_index == 1)
    return age;
else
    return name;
}

然后将其命名为ob1.get_prop<1>()

LIVE

在C ++ 17之前,您可以将template specialization应用为

template<>
const auto& myClass::get_prop<1>() const {
    return age;
}
template<>
const auto& myClass::get_prop<2>() const {
    return name;
}

LIVE