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

Lac*_*ith 6 c++ visual-c++ c++11

尝试在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测试了代码,他们都编译了代码而没有错误.

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

Sto*_*ica 7

这是MSVC的一个问题.隐含this参数具有cv限定条件.这就是为什么可以在cv-qualifier上重载成员函数的原因.在c'tor的主体中,this指向一个非const对象(初始化意味着我们必须修改对象).

这足以确定要调用的重载次数.

无论出于何种原因,MSVC感到困惑.但是如果通过this显式访问指针来调用成员函数,则混淆消失:

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

Live MSVC Example