【发布时间】:2019-03-06 23:11:06
【问题描述】:
标题有点混乱,但我的意思是这个具体案例:
template<class>
struct get_type_of_nontype;
template<class T, T Value, template<T> class Template>
struct get_type_of_nontype<Template<Value>> {
using type = T;
};
所以我可以这样使用它:
#include <type_traits>
template<int I>
class int_non_type {};
static_assert(
std::is_same<typename get_type_of_nontype<int_non_type<0>>::type, int>::value,
"T is deduced to be `int` as `template<T> class Template` is `template<int> class int_non_type`"
);
这在 C++17 中运行良好。在 C++14 中出现以下错误:
gcc 8:
<source>:5:8: error: template parameters not deducible in partial specialization:
struct get_type_of_nontype<Template<Value>> {
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<source>:5:8: note: 'T'
叮当声 7:
<source>:5:8: error: class template partial specialization contains a template parameter that cannot be deduced; this partial specialization will never be used [-Wunusable-partial-specialization]
struct get_type_of_nontype<Template<Value>> {
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<source>:4:16: note: non-deducible template parameter 'T'
template<class T, T Value, template<T> class Template>
^
然后他们都抱怨struct get_type_of_nontype<int_non_type<0>> 不完整,所以typename get_type_of_non_type<int_non_type<0>>::type 无法编译。
为什么在 C++14 和 C++17 之间会有所不同?这只是一个编译器错误吗?如果没有,有没有办法在 C++14 中做到这一点?
【问题讨论】:
标签: c++ templates c++14 c++17 template-templates