Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么模板参数的顺序对MS C++;这个例子中的编译器是什么?_C++_Templates_Visual C++_C++11 - Fatal编程技术网

C++ 为什么模板参数的顺序对MS C++;这个例子中的编译器是什么?

C++ 为什么模板参数的顺序对MS C++;这个例子中的编译器是什么?,c++,templates,visual-c++,c++11,C++,Templates,Visual C++,C++11,下面的代码在GCC中编译得很好,但在VisualStudio中会导致 错误C2782:“bool contains(常数T&,常数std::初始值设定项\u list &':模板参数“T”不明确请参见 “contains”可以是“const wchar\u t*”或“std::wstring” 但是,如果模板参数的顺序如下所示,则它可以编译并工作 模板 这是一个编译器错误吗 #include <string> #include <iostream> #include &l

下面的代码在GCC中编译得很好,但在VisualStudio中会导致

错误C2782:“
bool contains(常数T&,常数std::初始值设定项\u list
&
':模板参数“
T
”不明确请参见 “
contains
”可以是“
const wchar\u t*
”或“
std::wstring

但是,如果模板参数的顺序如下所示,则它可以编译并工作

模板

这是一个编译器错误吗

#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;
    }
  }
}
#包括
#包括
#包括
#包括

#include

我记得VS有一个bug,在某些情况下他们会进行双重扣减,我想这就是这里发生的事情。Clang也会以两种方式编译它,因此由于Clang+gcc同意,这很可能是一个VS错误。

我遇到了一个类似的问题,通过切换到最新的VS Pro版本解决了这个问题。我想这个错误在最新的VS pro版本中得到了解决,因为我记得在某个时候在更改日志中看到过它。

Yikes,我不想参与这个问题……没有什么奇怪的意义。。。i、 这是一个错误:)FWIW如果您显式指定作为
std::initializer\u list{…}
传递的第二个参数,这将编译。在这里更改模板参数的顺序相当于更改函数的形式参数的顺序,加上调用中实际参数的顺序。它不应该弄脏床垫。因此,这是一个错误。你可以在()报到。@cheers-sandhth.-Alf你确定吗?由于T和T2的用法不同,我发现这是违反直觉的,但我可能不知道您使用的“形式参数”的定义。