为什么要实例化此模板类专业化?

问题描述

在模板类的特化中,我调用一个不应编译的函数,但在我看来这无关紧要,因为我将 166 South Africa 164 USA 134 Spain 119 Sweeden 115 France 97 Russia 95 UK 9 Ukraine 5 Ireland dtype: object 作为模板参数传递,并且编译器不应在其中实例化该类第一名:

false

如果我传递 #include <iostream> template <typename T> struct outerstruct { template <bool b> struct innerStruct { }; template<> struct innerStruct<true> { using type = std::underlying_type_t<T>; }; template<> struct innerStruct<false> { }; innerStruct<false> member; // std::underlying_type_t cannot be called on a T,// even though that class shouldn't be instantiated // because I'm passing false (or I think) }; int main() { outerstruct<int> a; } 参数,为什么 innerStruct<true> 被实例化并且编译器试图调用 underlying_type

对我来说非常令人困惑,我可以通过向 false 添加一个模板参数,使其按照我想要的方式运行(即,让编译器忽略对 std::underlying_type_t调用),在这里添加innerClass

U

一个和第二个例子有什么区别?如果在第一个示例中我只调用#include <iostream> template <typename T> struct outerstruct { template <typename ExtraArg,bool b> struct innerStruct { }; template<typename ExtraArg> struct innerStruct<ExtraArg,true> { using type = std::underlying_type_t<T>; }; template<typename ExtraArg> struct innerStruct<ExtraArg,false> { }; //innerStruct<bValue> member; innerStruct<int,true> member; // Now it's working as I expected,// I can pass int or whatever argument to ExtraArg // and it compiles as long as I have 'false' }; int main() { outerstruct<int> a; }

,模板专业化 innerStruct<true> 有什么业务被实例化

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)