C++ 无法在MSVC 19.28上编译变量访问者访问权限
我试图在Visual Studio 2019上编译一个个人项目(使用MSVC 19.28编译器),但在C++ 无法在MSVC 19.28上编译变量访问者访问权限,c++,c++17,visual-studio-2019,variant,C++,C++17,Visual Studio 2019,Variant,我试图在Visual Studio 2019上编译一个个人项目(使用MSVC 19.28编译器),但在std::visit中遇到编译错误,我不理解: <source>(131): error C2653: '`global namespace'': is not a class or namespace name C:/data/msvc/14.28.29914/include\type_traits(1493): note: see reference to function t
std::visit
中遇到编译错误,我不理解:
<source>(131): error C2653: '`global namespace'': is not a class or namespace name
C:/data/msvc/14.28.29914/include\type_traits(1493): note: see reference to function template instantiation 'auto CommandLineOptionsParser<CmdLineOpts>::register_callback::<lambda_1>::()::<lambda_1>::operator ()<const _First&>(_T1) const' being compiled
with
[
_First=bool CmdLineOpts::* ,
_T1=bool CmdLineOpts::* const &
]
C:/data/msvc/14.28.29914/include\variant(1654): note: see reference to alias template instantiation 'std::_Variant_visit_result_t<CommandLineOptionsParser<CmdLineOpts>::register_callback::<lambda_1>::()::<lambda_1>,const std::variant<bool CmdLineOpts::* >&>' being compiled
<source>(120): note: while compiling class template member function 'void CommandLineOptionsParser<CmdLineOpts>::register_callback(const CommandLineOption &,std::variant<bool CmdLineOpts::* >)'
<source>(83): note: see reference to function template instantiation 'void CommandLineOptionsParser<CmdLineOpts>::register_callback(const CommandLineOption &,std::variant<bool CmdLineOpts::* >)' being compiled
<source>(142): note: see reference to class template instantiation 'CommandLineOptionsParser<CmdLineOpts>' being compiled
<source>(123): error C2672: 'visit': no matching overloaded function found
<source>(131): error C2893: Failed to specialize function template 'unknown-type std::visit(_Callable &&,_Variants &&...)'
C:/data/msvc/14.28.29914/include\variant(1654): note: see declaration of 'std::visit'
<source>(131): note: With the following template arguments:
<source>(131): note: '_Callable=CommandLineOptionsParser<CmdLineOpts>::register_callback::<lambda_1>::()::<lambda_1>'
<source>(131): note: '_Variants={const std::variant<bool CmdLineOpts::* > &}'
<source>(131): note: '<unnamed-symbol>=void'
Compiler returned: 2
MSVC似乎很难在模板上下文中合成带有指向成员参数的指针的lambda 我试图将其简化为MCVE,希望它抓住问题的本质:
template<class T>
bool test(int T::* t) {
return [](int T::* x) {
return true;
}(t);
}
struct A {
int a;
};
int main() {
return test<A>(&A::a);
}
模板
布尔测试(整数T::*T){
返回[](int T::*x){
返回true;
}(t) );
}
结构A{
INTA;
};
int main(){
返回测试):
(5):错误C2653:“'global namespace':不是类或命名空间名称
(13) :注:请参阅函数模板实例化“布尔测试”的参考
作为一种潜在的解决方法,可以尝试使用模板化的操作符()将lambda提取到函子类中,它似乎可以编译().将此code.cpp
文件作为一个bug归档。告诉他们它不在VS2019(包括16.10预览版2.1)上编译,而是使用clang cl和GCC进行编译。MSVC团队计划很快宣布完全支持C++20,所以他们应该能够修复此问题。@ChuckWalbourn I简化了代码,以消除对C++20和std::span的依赖性,问题继续存在。@ S.E.PhaneJeNeL将其报告为MSVC C++编译器的bug。谢谢@ RuStux,我打开了一张感谢您的MCVE的票,并且他们已经在进行修复了。
template<class T>
bool test(int T::* t) {
return [](int T::* x) {
return true;
}(t);
}
struct A {
int a;
};
int main() {
return test<A>(&A::a);
}