別のヘルパー クラスを使用せずに、テンプレートを特殊化してこの動作を抽象化する (論理 OR を使用して型を特殊化する) 可能性はありますか。
int または char を同じクラスに渡すときに特殊化します。
template<typename K>
struct test
{
};
template<>
struct test<int or char>
{
};
ありがとう。
CB
別のヘルパー クラスを使用せずに、テンプレートを特殊化してこの動作を抽象化する (論理 OR を使用して型を特殊化する) 可能性はありますか。
int または char を同じクラスに渡すときに特殊化します。
template<typename K>
struct test
{
};
template<>
struct test<int or char>
{
};
ありがとう。
CB
これには 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
};
あなたの質問は漠然としすぎていますが、このようなことについて話していると思いますか?
template <typename T>
struct A
{
//...
}
template<B>
struct A
{
//...
}
この場合、特定のタイプのテンプレート化されたテンプレート構造体がどのように動作するかを指定します
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
};