-4

別のヘルパー クラスを使用せずに、テンプレートを特殊化してこの動作を抽象化する (論理 OR を使用して型を特殊化する) 可能性はありますか。

int または char を同じクラスに渡すときに特殊化します。

template<typename K>
struct test
{

};

template<>
struct test<int or char>
{

};

ありがとう。

CB

4

3 に答える 3

2

これには C++11 の型特性を使用できます (または、C++11 をまだ持っていない場合は、Boost の型特性を使用します)。

#include <type_traits>

template <typename K, bool special = std::is_same<K, char>::value || std::is_same<K, int>::value>
struct A
{
  // general case
};

template <typename K>
srtuct A<K, true>
{
  //int-or-char case
};
于 2013-08-19T13:56:00.253 に答える
0

あなたの質問は漠然としすぎていますが、このようなことについて話していると思いますか?

template <typename T>
struct A
{
    //...
}

template<B>
struct A
{
    //...
}

この場合、特定のタイプのテンプレート化されたテンプレート構造体がどのように動作するかを指定します

于 2013-08-19T13:51:40.563 に答える
0

C++ は部分的な特殊化をサポートしています。問題を解決する最も簡単な方法 (これはあなたの問題だと思います) は、int または char、ala の構造体テストを部分的に特殊化することです。

template <typename T> struct test
{
    // For arbitrary type T...
};

template <>
struct test<int>
{
    // definition for when T = int
};

template <>
struct test<char>
{
    // definition for when T = char
};
于 2013-08-19T13:55:23.063 に答える