2016-08-16 50 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 ++編譯器的診斷指導我欠工作代碼。

雖然我認爲這仍然很醜。

一個可能更好的解決方案是重新考慮整個事情並改變設計。