概述最近我设计了元类型和允许编译时类型连接的可能 *** 作: #include <tuple>template<template<typename...> typename T>struct MetaTypeTag{};/*variable template helper*/template<template<typename...> typename T>constexpr MetaTyp 最近我设计了元类型和允许编译时类型连接的可能 *** 作: @H_301_2@#include <tuple>template<template<typename...> typename T>struct MetaTypeTag{};/*variable template helper*/template<template<typename...> typename T>constexpr MetaTypeTag<T> Meta_type_tag = {};template<typename T>struct TypeTag{};/*comparison*/template<typename T>constexpr bool operator==(TypeTag<T>,TypeTag<T>) { return true; }template<typename T,typename U>constexpr bool operator==(TypeTag<T>,TypeTag<U>) { return false; }/*variable template helper*/template<typename T>constexpr TypeTag<T> type_tag = {};template<template<typename...> typename T,typename... Ts>constexpr TypeTag<T<Ts...>> combine(MetaTypeTag<T>,TypeTag<Ts>...){ return {};}int main(){ constexpr auto combined_tag = combine(Meta_type_tag<std::tuple>,type_tag<int>,type_tag<float>); static_assert(combined_tag == type_tag<std::tuple<int,float>>,"");}
没有模板参数的std :: tuple不能用作类型,但仍可能出现在模板模板参数中.
现在,如果我们试图更进一步,问题是是否有任何方法来统一struct MetaTypeTag和struct TypeTag,因为它们都是带有一个模板参数的空类,或者至少可以使用相同的变量模板type_tag但根据类型类别重定向到不同的类?所以我会想象这样的事情:
@H_301_2@template<???>constexpr auto type_tag = ????{};//use with 'incomplete type'type_tag<std::tuple> //MetaTypeTag<std::tuple>//use with regular typetype_tag<int> //TypeTag<int>
我尝试了所有可能的方法 – 重新定义,显式特化,部分特化,可选模板参数,条件使用别名,但没有工作.我曾希望C 17的模板< auto>会有所帮助,但事实证明,一个只适用于非类型.
@H_301_13@解决方法
the question is whether there is any way to unify struct MetaTypeTag
and struct TypeTag
,since they are both empty classes with one template parameter
我不这么认为.
我能想象的最好的是简化一点(非常一点)你的代码定义了一些重载的constexpr函数,比如说getTag()
@H_301_2@template <typename T>auto constexpr getTag () { return TypeTag<T>{}; }template <template <typename ...> typename T>auto constexpr getTag () { return MetaTypeTag<T>{}; }
所以你可以调用getTag< T>(),其中T是一个类型或模板.
所以你可以调用combine(),如下所示
@H_301_2@constexpr auto combined_tag = combine(getTag<std::tuple>(),getTag<int>(),getTag<float>());
但我认为这不是一个很大的进步.
总结
以上是内存溢出为你收集整理的c – Q:采用普通类型或模板模板参数的模板类全部内容,希望文章能够帮你解决c – Q:采用普通类型或模板模板参数的模板类所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
评论列表(0条)