【发布时间】:2020-02-20 10:39:18
【问题描述】:
由this question触发,我写了一个模板,允许将模板转换为具有不同参数的模板:
template< template <int A,char B,bool> typename T>
struct Add_true {
template <int A,char B> using type = T<A,B,true>;
};
背景:目的是将template<int A, char B, bool C> class A2{}; 作为模板模板参数传递给template<template<int A, char B> typename T> class A1 {};。我学习了 C++11 的模板。一旦我习惯于编写接受类型和“返回”类型的类型/函数,而不是编写接受值和返回值的函数,我对代码的思考就发生了很大的转变。我从来没有想过,编写给定模板“返回”不同模板的模板也非常简单。问题是我一点也不知道如何在没有别名模板的情况下编写上述或类似的内容,这些模板仅在 C++11 之后才可用。以下是始终可能发生的事情。给定
template <int A,char B, bool C> struct Foo{};
我可以通过以下方式将布尔参数“绑定”到Foo:
template <int A,char B>
struct Add_true_to_Foo {
typedef Foo<A,B> type;
};
但是现在Add_true_to_Foo<A,B>::type 不是模板,如果没有 C++11,我一点也不知道如何编写一个不仅适用于 Foo 也适用于
template <int A,char B, bool C> struct Bar{};
也许我遗漏了一些明显的东西。我的问题是
在C++11之前可以写出和上面Add_true等价的东西吗?
【问题讨论】:
标签: c++ templates c++03 template-templates