我试图将这个问题的最佳答案放入我自己的项目中,并且无法让 gcc 编译它(在 RHEL linux 上使用“-std=gnu++0x”)。我已经对该原始代码进行了一些更改,因此我的最终结果如下:
#include <string>
#include <vector>
#include <fstream>
#include <cerrno>
#include <cstring>
#include <array>
#include <algorithm>
template <typename T0, typename T1, size_t N> bool operator *(const T0& lhs, const std::array<T1, N>& rhs)
{
return std::find(rhs.begin(), rhs.end(), lhs) != rhs.end();
}
template <class T0, class...T> std::array<T0, 1+sizeof...(T)> in(T0 arg0, T...args)
{
return {{arg0, args...}};
}
其中一些包含将不相关,我只是向您展示所有这些内容。我正在使用这样的功能:
if (1 *in(1,2,3))
编译时,gcc 在“in”的“return”行给出如下错误:
error: could not convert '{{arg0, args#0, args#1}}' to 'std::array<int, 3u>'
任何人都可以解释为什么会这样吗?
我之前在 C++11 中做得不多,所以在试图找出问题所在时我有点迷失了。我已经尝试在“args”位周围使用不同数量的 {},但到目前为止无济于事。
谢谢你提供的所有帮助。