这是另一个问题的后续行动.它指的是同样的问题(我希望),但使用一个完全不同的例子来说明它.原因是在前面的示例中,只有实验性GCC 4.9因编译器错误而失败.在此示例中,Clang和GCC 4.8.1也以不同方式失败:Clang产生意外结果,GCC 4.8.1报告不同的错误消息.
上一个问题的答案或多或少地说代码是有效的,问题在于GCC的实验版本.但这个结果让我更加怀疑.几个月来我一直困扰着我怀疑有相关(或相同)的问题,这是我第一次有一个小的具体例子来说明.
所以,这里有一些代码.首先,一些通用代码将SFINAE应用于由可变参数模板别名元函数指定的任意测试F
:
#includeusing namespace std; using _true = integral_constant ; using _false = integral_constant ; template using pass = _true; template class F> struct test { template static _false _(...); template static pass > _(int); }; template class F, typename... A> using sfinae = decltype(test ::template _ (0));
第二,一个特定的测试,检查给定的类是否定义了一个名为的类型type
:
templateusing type_of = typename T::type; template using has_type = sfinae ;
最后一个例子:
struct A { using type = double; }; int main() { cout << has_type() << ", "; cout << has_type () << endl; }
预期的结果是0, 1
.克朗说0, 0
.GCC 4.8.1说
tst.cpp: In substitution of ‘templateusing type_of = typename T::type [with T = A ...]’: tst.cpp:15:51: required from ‘struct test ’ tst.cpp:19:67: required by substitution of ‘template class F, class ... A> using sfinae = decltype (test:: _(0)) [with F = type_of; A = {T}]’ tst.cpp:24:58: required from here tst.cpp:23:56: error: ‘A ...’ is not a class, struct, or union type template using type_of = typename T::type; ^
和GCC 4.9说
tst.cpp:19:67: required by substitution of ‘template class F, class ... A> using sfinae = decltype (test:: _(0)) [with F = type_of; A = {T}]’ tst.cpp:24:58: required from here tst.cpp:15:51: error: pack expansion argument for non-pack parameter ‘T’ of alias template ‘templateusing type_of = typename T::type’ template static pass > _(int); ^
(行号可能有所不同).所以,一切都以不同的方式失败了.
现在,这是一个解决方法.Metafunction car
选择给定包中的第一种类型,然后将测试重新定义为type_of2
,现在是可变的:
templatestruct car_t; template using car = type_of >; template struct car_t { using type = T; }; template using type_of2 = typename car ::type; template using has_type2 = sfinae ; int main() { cout << has_type2 () << ", "; cout << has_type2 () << endl; }
现在所有三个编译器都0, 1
按预期说.有趣的是,对于任何版本的GCC,我们必须删除has_type
(即使我们不使用它)并且只留下has_type2
; 否则我们有类似的错误.
总结:我看到一个模板的问题,期望表单的可变参数模板参数
templateclass F
我们实际上给出了表单的非变量模板别名作为输入
templateusing alias = // ... anything including T or not
最后调用F
就好像它是可变的:
F
到目前为止的意见认为这是有效的,但现在似乎有三个编制者不同意.所以问题又来了:它有效吗?
对我而言很重要,因为我有几十个现有代码文件,基于这个假设是有效的,现在我还需要重新设计(因为这些编译器存在实际问题),但确切的重新设计将取决于答案.