2016-08-16 43 views
0

我正在将一些Julia代码移植到C++并且遇到问题。更糟糕的是,我对C++术语不是很熟悉,所以一直没有能够按照我的方式进行操作。我试图弄清楚如何引用一个模板(这是一个模板别名?),所以我可以稍后使用它,即引用一个类型。我已经尝试了各种咒语,涉及using,typenametemplate,但似乎没有任何东西让我的编译器感到高兴。我设法生产出了我想要的东西(见下文),但它很讨厌。有没有更好的办法?任何合理的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> 
+1

[代码评论](http://codereview.stackexchange.com)? – Rakete1111

+0

询问“有没有更好的方法?”的最佳方式?用简单的英语来解释你在这里想要完成的事情。否则,你基本上要求的地方是对现有代码进行反向工程,找出它想要完成的工作,然后只尝试找出更好的方法。通过使前两步不必要,您可以节省相当多的时间。 –

+0

所有这些'template '在其他C++代码中都非常不寻常。你想用这个做什么? –

回答

1

而不是

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 ++编译器的诊断指导我欠工作代码。

虽然我认为这仍然很丑。

一个可能更好的解决方案是重新考虑整个事情并改变设计。