Dre*_*rek 5 c++ templates c++11
我试图让返回类型与基类中的返回类型doSomeMethod()
相同,operator()
但如果它被声明为protected,则编译器会拒绝代码error: no type named 'type' in 'std::result_of'
.它是公共的,但我想知道我是否可以让它在受保护的情况下工作.
这是重现错误的简单代码.
#include <type_traits>
class base
{
protected:
int operator()(){return 1;};
};
class child : private base
{
auto static doSomeMethod()
-> typename std::result_of<base()>::type
{
return 1;
}
};
Run Code Online (Sandbox Code Playgroud)
编辑:
好的,谢谢Kerrek SB和DietmarKühlr的解决方案和解释.在玩完之后,我发现这个解决方案更具可读性(至少在我的实际情况下,child
模板类和base
模板参数之一)更可靠.但它似乎有点违背你的解释.或者只是std::result_of<>
在这种情况下打破方式?
#include <type_traits>
class base
{
protected:
double operator()(){ return 1; };
int operator()(int i){ return i; };
};
class child : private base
{
public:
auto operator()()
-> decltype(base::operator()())
{
return base::operator()();
}
auto operator()(int i)
-> decltype(base::operator()(std::declval<int>()))
{
return base::operator()(i);
}
};
Run Code Online (Sandbox Code Playgroud)
谢谢你的时间.