2017-06-09 7 views
0

私が直面している問題は次のとおりです。不明な型の配列arrayにいくつかのデータが格納されています。しかし、関数array_getDataType()は、配列が与えられた場合、UBIT8の整数を返します。UBIT8はどこかで定義された定数です。これは基本的に配列に格納された要素の型がunsigned charであることを意味します。私は、定義された定数と実際の型の間にマップを作成する方法があるかどうか、 "BIT8"を取り込み、 "unsigned char"を返すものがあるかどうか疑問に思っていました。私は次の方法でテンプレートとは逆をする方法があることを知っています。C++で整数型キーから変数型へのテンプレート化された構造体またはマップを作成

template< typename T > 
struct type2Int 
{ 
    enum { id = 0 }; 
}; 
template<> struct type2Int<unsigned char> { enum { id = UBIT8 }; }; 

、これは後に、その後

type.id 

が、私は反対のことを行う方法を思っていたUBIT8の定義は何だろう

type2Int<unsigned char> type; 

として使用することができます。

+0

を使用することができますがデータ型を取得していますか? –

答えて

1

私はその逆をどうやって行うのかと思いました。

同様に、テンプレートの特殊化を使用します。例によって

#include <iostream> 

template <std::size_t> 
struct int2type; 
// { using type = void; }; ??? 

template <> 
struct int2type<0U> 
{ using type = char; }; 

template <> 
struct int2type<1U> 
{ using type = int; }; 

template <> 
struct int2type<2U> 
{ using type = long; }; 

int main() 
{ 
    static_assert(std::is_same<typename int2type<0>::type, char>::value, "!"); 
    static_assert(std::is_same<typename int2type<1>::type, int>::value, "!"); 
    static_assert(std::is_same<typename int2type<2>::type, long>::value, "!"); 
} 

あなたがC++ 11(これなしusing)を使用できない場合、あなたはあなたが後にどうするつもりです何が良い、古いtypedef

template <std::size_t> 
struct int2type; 

template <> 
struct int2type<0U> 
{ typedef char type; }; 

// ... 
関連する問題