我试图使我的代码在Linux上编译为与Windows兼容。这是我的情况的简化(在实际情况下,我可以使用多种类型的变体。):
#include "stdafx.h"
#include <variant>
#include <functional>
#include <string>
using namespace std;
auto searcher(const string& s) {
return boyer_moore_searcher(s.begin(), s.end());
}
int main()
{
string pattern = "ABC";
variant<decltype(searcher(""))> v{ searcher(pattern) };
}
使用MSVC 2017编译时,出现以下错误:
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\functional(2603): error C2440: 'initializing': cannot convert from 'const std::boyer_moore_searcher<std::_String_const_iterator<std::_String_val<std::_Simple_types<_Ty>>>,std::hash<char>,std::equal_to<void>>' to 'void *'
1> with
1> [
1> _Ty=char
1> ]
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\functional(2603): note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\functional(2601): note: while compiling class template member function 'std::boyer_moore_searcher<std::_String_const_iterator<std::_String_val<std::_Simple_types<_Ty>>>,std::hash<char>,std::equal_to<void>>::boyer_moore_searcher(const std::boyer_moore_searcher<std::_String_const_iterator<std::_String_val<std::_Simple_types<_Ty>>>,std::hash<char>,std::equal_to<void>> &) noexcept'
1> with
1> [
1> _Ty=char
1> ]
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\variant(537): note: see reference to function template instantiation 'std::boyer_moore_searcher<std::_String_const_iterator<std::_String_val<std::_Simple_types<_Ty>>>,std::hash<char>,std::equal_to<void>>::boyer_moore_searcher(const std::boyer_moore_searcher<std::_String_const_iterator<std::_String_val<std::_Simple_types<_Ty>>>,std::hash<char>,std::equal_to<void>> &) noexcept' being compiled
1> with
1> [
1> _Ty=char
1> ]
1>d:\users\narut\source\repos\consoleapplication2\consoleapplication2\consoleapplication2.cpp(12): note: see reference to class template instantiation 'std::boyer_moore_searcher<std::_String_const_iterator<std::_String_val<std::_Simple_types<_Ty>>>,std::hash<char>,std::equal_to<void>>' being compiled
1> with
1> [
1> _Ty=char
1> ]
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\functional(2603): error C2439: 'std::boyer_moore_searcher<std::_String_const_iterator<std::_String_val<std::_Simple_types<_Ty>>>,std::hash<char>,std::equal_to<void>>::_Data': member could not be initialized
1> with
1> [
1> _Ty=char
1> ]
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\functional(2629): note: see declaration of 'std::boyer_moore_searcher<std::_String_const_iterator<std::_String_val<std::_Simple_types<_Ty>>>,std::hash<char>,std::equal_to<void>>::_Data'
1> with
1> [
1> _Ty=char
1> ]
同一代码使用gcc上的libstdc ++实现编译。我尝试用clang进行libc ++实验,它也可以编译。这是MSVC的实现错误,还是没有要求variant
才能与此特定类一起工作的C ++标准?