【问题标题】:C++11 decltype and data members [duplicate]C ++ 11 decltype和数据成员[重复]
【发布时间】:2014-02-26 16:24:02
【问题描述】:

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

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'

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

【问题讨论】:

  • clang++3.5 和 g++4.8.1 接受它。这是一个强烈的暗示,它是 MSVC 中的一个错误。
  • 我认为这确实是 MSVC 中的一个错误。 trailing-return-type 不是 declarator-id 的一部分,而是跟随它。因此,名称查找是(=应该)与inside成员函数体的定义相同。

标签: c++ c++11


【解决方案1】:

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

尝试类似:

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

【讨论】:

  • 不,也不行
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多