2016-08-16 8 views
0

私はC++にいくつかのジュリアのコードを移植に取り組んでいると問題に遭遇してきました。事態を悪化させるために、私はC++の命名法にあまりよく慣れていないので、googleを抜け出せませんでした。構文テンプレート(別名?)を参照する

基本的に、私はテンプレートを参照する方法を把握しようとしている(これは、テンプレートの別名である?)ので、私は種類を参照するために、すなわち、後でそれを使用することができます。私はusingtypename、およびtemplateを含む様々な呪文を試してみたが、何も私のコンパイラを幸せに思いません。私は、私が欲しいものをしているもの(下記参照)を作り出すことができましたが、それはかなり厄介です。より良い方法がありますか?どんな妥当なC++でも問題ありません。

次のコードは、私はちょうどtypenameのいくつかの組み合わせを試してみました

// 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; 
} 

しようとする代わりに

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; 
} 

#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

"良い方法がありますか?"あなたがここで成し遂げようとしていることを平易な英語で説明することでしょう。それ以外の場合は、本質的に求めているのは、既存のコードをリバースエンジニアリングし、達成しようとしていることを把握し、***のみで***よりよいアプローチを見つけようとします。最初の2つのステップを不要にすることで、かなりの時間を節約できます。 –

+0

この 'template 'は、他のC++コードでは非常に珍しいことです。あなたはそれで何をしようとしていますか? –

答えて

1

を達成しようとしているものの最低限の例です。 templateなどとg ++コンパイラの診断を私に任せる賞の作業コード。

私は、これはしかし、まだ醜いだと思います。

A、おそらくより良い解決策は、全体のことを再考し、デザインを変更することです。

関連する問題