[英]Why does the order of template parameters matter to the MS C++ compiler in this example?
The following code compiles fine in GCC but in Visual Studio it results in 以下代码在GCC中编译良好,但在Visual Studio中导致
error C2782: '
bool contains(const T &,const std::initializer_list<T2>
&)
' : template parameter 'T
' is ambiguous see declaration of 'contains
' could be 'const wchar_t *
' or 'std::wstring
'错误C2782:'
bool contains(const T &,const std::initializer_list<T2>
&)
':模板参数'T
'模棱两可,请参见'contains
'的声明可能是'const wchar_t *
'或'std::wstring
'
It does however compile and work if the order of the template parameters is given as 但是,如果模板参数的顺序为
template<typename T2, typename T>
Is this a compiler bug? 这是编译器错误吗?
#include <string>
#include <iostream>
#include <set>
#include <initializer_list>
#include <algorithm>
template<typename T, typename T2>
bool contains(T const& value, std::initializer_list<T2> const& set)
{
return std::find(std::begin(set), std::end(set), value) != std::end(set);
}
int main(void)
{
std::set<std::wstring> values = { L"bar", L"not" };
for (std::wstring val : values) {
std::wcout << "\"" << val << "\" ";
if (contains(val, { L"foo", L"bar", L"baz", L"doom" })) {
std::wcout << "found" << std::endl;
}
else {
std::wcout << "not found" << std::endl;
}
}
}
Edit: I have created a bugreport: https://connect.microsoft.com/VisualStudio/feedbackdetail/view/982338/template-parameter-order-matters 编辑:我创建了一个错误报告: https : //connect.microsoft.com/VisualStudio/feedbackdetail/view/982338/template-parameter-order-matters
I remember that VS has a bug where they would do double-deduction in certain scenarios, and I think that's what's happening here. 我记得VS有一个错误,他们会在某些情况下进行双重推论,我认为这就是发生的情况。 Clang also compiles it both ways, so since clang + gcc agree, it's likely a VS bug.
Clang也同时进行两种方式的编译,因此,由于clang + gcc同意,因此很可能是VS错误。
I had a similar problem which was resolved by switching to the latest VS Pro version. 我有一个类似的问题,可以通过切换到最新的VS Pro版本来解决。 I think this bug was addressed in the latest VS pro version as I remember seeing it in a change-log at some point.
我认为这个错误已在最新的VS专业版中得到解决,因为我记得在某个时候在更改日志中看到过该错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.