【发布时间】:2017-11-22 10:24:49
【问题描述】:
g++ 7.2.0 中的以下代码 compiles successfully(编译标志为 -std=c++14 -Wall -Wextra -Werror -pedantic-errors),但在 clang++ 5.0.0 中编译失败(带有相同的标志,-std=c++14 -Wall -Wextra -Werror -pedantic-errors) 和 vc++ 15.4(编译标志是 /EHsc /Za /std:c++14 /permissive-):
template <template <typename...> class Functor, typename... FixedArguments>
struct apply
{
template <typename... FreeArguments>
using type = Functor<FixedArguments..., FreeArguments...>;
};
template <typename, typename>
struct Bar{};
template <template <typename...> class>
struct Foo{};
int main()
{
(void)Foo<apply<Bar, int, char>::type>{};
}
哪种编译器行为符合标准?如何将 apply 这样的模板更改为也可以在 clang++ 上编译?
clang++ 错误信息:
5 : <source>:5:15: error: too many template arguments for class template 'Bar' using type = Functor<FixedArguments..., FreeArguments...>; ^ ~~~~~~~~~~~~~~~~~ 16 : <source>:16:15: note: in instantiation of template class 'apply<Bar, int, char>' requested here (void)Foo<apply<Bar, int, char>::type>{}; ^ 9 : <source>:9:8: note: template is declared here struct Bar{};
vc++ 错误信息:
5 : <source>(5): error C2977: 'Bar': too many template arguments 9 : <source>(9): note: see declaration of 'Bar' 16 : <source>(16): note: see reference to class template instantiation 'apply<Bar,int,char>' being compiled
【问题讨论】:
-
Clang 给出什么错误?
-
@piwi clang++ 和 vc++ 错误消息可在 the link to the godbolt.org in the question 获得。我也将它们添加到问题中。
-
我的不好在发表评论之前没有看到。
-
[temp.res]/8.3。这是格式错误的 NDR。但请参阅core issue 2067。
-
@T.C.感谢您的参考!但是如何修复这样的代码才能编译呢?
标签: c++ templates variadic-templates template-meta-programming partial-application