c++ - Q : Template class that takes either a normal type or a template template argument

标签 c++ c++11 metaprogramming template-meta-programming constexpr

最近我设计了元类型和允许编译时类型连接的可能操作:

#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 MetaTypeTagstruct TypeTag ,因为它们都是具有一个模板参数的空类,或者至少可以使用相同的变量模板 type_tag但根据类型类别重定向到不同的类?所以我会想象这样的事情:

template<???>
constexpr auto type_tag = ????{};

//use with 'incomplete type'
type_tag<std::tuple> //MetaTypeTag<std::tuple>
//use with regular type
type_tag<int> //TypeTag<int>

我尝试了所有可能的方法——重新定义、显式特化、部分特化、可选模板参数、有条件地使用别名,但都没有奏效。我曾希望 C++17 的 template<auto>会有所帮助,但事实证明,一个仅适用于非类型。

最佳答案

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()

template <typename T>
auto constexpr getTag ()
 { return TypeTag<T>{}; }

template <template <typename ...> typename T>
auto constexpr getTag ()
 { return MetaTypeTag<T>{}; }

所以你可以调用getTag<T>()其中 T是类型或模板。

所以你可以调用combine()如下

constexpr auto combined_tag
   = combine(getTag<std::tuple>(), getTag<int>(), getTag<float>());

但我不认为这是一个很大的改进。

关于c++ - Q : Template class that takes either a normal type or a template template argument,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45802194/

相关文章:

c++ - 通过函数引用检索返回类型?

java - 使用 void* 作为 Java 对象的 C++ 等价物

c++ - 在链接到动态库的静态库中导出符号

c++ - OpenGL 无法使用 Shaders Mac 进行渲染

c++ - 优化开关——它们到底做了什么?

c++ - 自定义双向迭代器的 reverse_iterator 上的 for_each 需要 OutputIterator

c++ - 基于范围的for循环可以在一个范围内工作吗

c++ - 我的编译器允许 "T&..."。这是一个扩展吗?

ruby - 如何在 Ruby 中像这样设置哈希实例的值?

metaprogramming - 在表达式内部通过引用传递表达式