小编Lac*_*ith的帖子

对成员函数的模糊调用在lambda中捕获了它

尝试在lambda中调用成员函数捕获时遇到了问题this.有一个const和非const版本的函数,它是在一个类型上模板化的.

以下代码演示了错误:

struct TEST
{
  template <typename T>
  void test() {}

  template <typename T>
  void test() const {}

  TEST()
  {
    [this]()
    {
      test<void>();
    }();
  }
};
Run Code Online (Sandbox Code Playgroud)

消息:http://rextester.com/MLU2098

source_file.cpp(13): error C2668: 'TEST::test': ambiguous call to overloaded function
source_file.cpp(7): note: could be 'void TEST::test<void>(void) const'
source_file.cpp(4): note: or       'void TEST::test<void>(void)'
source_file.cpp(13): note: while trying to match the argument list '()'
Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23506 for x64
Run Code Online (Sandbox Code Playgroud)

我不确定这种行为是否正确,只是Microsoft编译器的一个问题,所以我在编译器资源管理器中用gcc和clang测试了代码,他们都编译了代码而没有错误.

哪个编译器在这里显示正确的行为?

c++ visual-c++ c++11

6
推荐指数
1
解决办法
322
查看次数

标签 统计

c++ ×1

c++11 ×1

visual-c++ ×1