下面的最小示例在 MSVC 17 上编译得很好,但在 GCC 8.2 上会产生编译错误。哪个编译器是对的?这段代码在 C++17 中是否正确?
#include <iostream>
class A
{
public:
A() = default;
protected:
void foo(int x)
{ std::cout << x << std::endl; }
};
class B : private A
{
using Method_t = void (B::*)(int);
using A::foo;
template <Method_t M>
void baz()
{ (this->*M)(42); }
public:
B() = default;
void bar()
{ baz<&B::foo>(); }
};
int main()
{
B().bar();
}
Run Code Online (Sandbox Code Playgroud)
GCC 错误是:
mwe.cpp:29:20: error: could not convert template argument '&A::foo' from 'void (A::*)(int)' to …Run Code Online (Sandbox Code Playgroud)