引用模板(别名?)的语法
Syntax to refer to a template (alias?)
我正在将一些Julia代码移植到c++中,并且遇到了一个问题。更糟糕的是,我对c++的命名法不太熟悉,所以没能在谷歌上找到出路。
基本上,我想弄清楚如何引用模板(这是模板别名吗?),以便我可以稍后使用它,即引用类型。我尝试了各种咒语涉及using
, typename
和template
,但似乎没有什么让我的编译器高兴。我已经设法生产了一些我想要的东西(见下文),但它很讨厌。有没有更好的办法?任何合理的c++都可以。
下面的代码是我试图实现的最小示例
#include <iostream>
#include <type_traits>
template<int n, template<int> class T>
int unwrap(T<n>& x) { return n; }
template<int n>
struct A { static const char name = 'A'; };
template<int n>
struct B { static const char name = 'B'; };
template<bool flag>
struct promote_if {
template<int n> using type = A<n>;
};
template<>
struct promote_if<true> {
template<int n> using type = B<n>;
};
template<int m, int n,
template<int> class X,
template<int> class Y,
template<int> class Z>
struct Stuff { static const int seven = 7; };
// Add type parameters and return
// B<m + n> if X or Y is a B,
// otherwise return A<m + n>
template<int m, int n, template<int> class X, template<int> class Y>
auto add(X<m>& x, Y<n>& y) {
static const bool any = std::is_base_of<B<m>, X<m>>::value || std::is_base_of<B<n>, Y<n>>::value;
// This works, but is gross
std::cout << Stuff<m,n,promote_if<any>::template type,X,Y>::seven << "n";
typename promote_if<any>::template type<m + n> z;
// Something like this is what I'm trying to achieve
// using T = typename promote_if<any>::template type;
// T<m + n> z;
// std::cout << Stuff<m,n,T,X,Y>::seven << "n";
return z;
}
int main(int argc, char const *argv[]) {
A<1> a;
B<2> b;
auto c = add(a, a);
std::cout << "c = add(a, a) = " << c.name << "<" << unwrap(c) << ">n";
auto d = add(a, b);
std::cout << "d = add(a, b) = " << d.name << "<" << unwrap(d) << ">n";
return 0;
}
// Output
// Stuff is 7
// c = add(a, a) = A<2>
// Stuff is 7
// d = add(a, b) = B<3>
代替
template<int m, int n, template<int> class X, template<int> class Y>
auto add(X<m>& x, Y<n>& y) {
static const bool any = std::is_base_of<B<m>, X<m>>::value || std::is_base_of<B<n>, Y<n>>::value;
// This works, but is gross
std::cout << Stuff<m,n,promote_if<any>::template type,X,Y>::seven << "n";
typename promote_if<any>::template type<m + n> z;
// Something like this is what I'm trying to achieve
// using T = typename promote_if<any>::template type;
// T<m + n> z;
// std::cout << Stuff<m,n,T,X,Y>::seven << "n";
return z;
}
试
// Add type parameters and return
// B<m + n> if X or Y is a B,
// otherwise return A<m + n>
template<int m, int n, template<int> class X, template<int> class Y>
auto add(X<m>& x, Y<n>& y) {
static const bool any = std::is_base_of<B<m>, X<m>>::value || std::is_base_of<B<n>, Y<n>>::value;
using T = promote_if<any>;
typename T::template type<m + n> z;
std::cout << Stuff<m,n,T::template type,X,Y>::seven << "n";
return z;
}
我只是尝试了typename
和template
等的一些组合,让g++编译器的诊断引导我走向工作代码。
我认为这仍然是丑陋的。
一个更好的解决方案可能是重新考虑整个事情并改变设计。
相关文章:
- 1d 智能指针不适用于语法 (*)++
- 部分定义/别名模板模板参数
- 助记符和指向成员语法的指针
- 有人能分解一下这个c++模板的语法吗
- C++避免重复声明的语法是什么
- QMetaObject invokeMethod的基于函数指针的语法
- 如何在C++20中创建模板别名的推导指南
- 这个语法std::class<>{}(arg1, arg2) 在C++中是什么意思?
- 为什么包含windows.h会产生语法错误,从而阻止类的实例化?(C2146,C2065)
- 告诉c++编译器该参数没有别名
- 单独定义模板化嵌套类方法的正确语法
- 别名模板和令人困惑的语法
- 应用于类型别名声明的 [[maybe_unused]] 属性的语法
- 具有"using"语法的固定大小数组别名
- 模板别名上下文中的模板<>模板<>语法有什么用?
- Doxygen 对 C++11 模板别名的支持("using"语法)?
- 定义类型别名时出现语法错误
- 别名而不是糖语法的新变量
- 引用模板(别名?)的语法
- 用于声明具有别名的类层次结构的Cython语法