C ++ 11 decltype和数据成员

时间:2014-02-26 07:56:01

标签: c++ c++11

以下代码编译时没有任何错误:

class foo
{
public:
  void some_func() {}
};

class bar
{
private:
  foo instance;

public:
  auto some_func() -> decltype(instance.some_func()) {}
};

int main() {}

以下代码无法在MSVC-12.0中编译:

class foo
{
public:
  void some_func() {}
};

class bar
{
private:
  foo instance;

public:
  auto some_func() -> decltype(instance.some_func());
};

auto bar::some_func() -> decltype(instance.some_func()) {}

int main() {}

它给了我以下错误:

error C2228: left of '.some_func' must have class/struct/union
error C2556: 'int bar::some_func(void)' : overloaded function differs only by return type from 'void bar::some_func(void)'
 : see declaration of 'bar::some_func'
error C2371: 'bar::some_func' : redefinition; different basic types
 : see declaration of 'bar::some_func'

我做错了什么?我该如何解决?

1 个答案:

答案 0 :(得分:0)

我现在没有VS12,所以我有点猜测。在bar :: some_func的声明中,当在类外声明时,您无权访问'instance'成员。

尝试类似:

auto bar::some_func() -> decltype(bar::instance.some_func()) {}