私は、マネージド・ネイティブ・コンバータをC++/cliに実装しようとしています。私はこのためにテンプレートを使用しようとしているので、変換する約20種類があります。 問題は、値の型と参照の型を別々に扱う必要があることです。ここでC++の制約付きテンプレートの特殊化
が(。このコードはOKである。少なくとも、それはコンパイル)私が実装しようとしているものです:
#define val_t_constraint(T) std::enable_if_t<std::is_integral<T>::value || std::is_floating_point<T>::value, T>
#define ref_t_constraint(T) std::enable_if_t<!std::is_integral<T>::value && !std::is_floating_point<T>::value, T>
template<class TElementIn, class TElementOut = TElementIn>
static val_t_constraint(TElementOut) convert(const TElementIn& native)
{
return (TElementOut)native;
}
template<class TElementIn, class TElementOut = TElementIn>
static ref_t_constraint(TElementOut)^ convert(const TElementIn& native)
{
return gcnew TElementOut();
}
template<class TElementIn, class TElementOut = TElementIn>
static array<val_t_constraint(TElementOut)>^ convert(const std::vector<TElementIn>& native)
{
auto arr = gcnew array<TElementOut>(1);
arr[0] = convert<TElementIn, TElementOut>(native[0]);
return arr;
}
template<class TElementIn, class TElementOut = TElementIn>
static array<ref_t_constraint(TElementOut)^>^ convert(const std::vector<TElementIn>& native)
{
auto arr = gcnew array<TElementOut^>(1);
arr[0] = convert<TElementIn, TElementOut>(native[0]);
return arr;
}
しかし、私はこのような例のために、いくつかのテンプレートを特化しようとしている:
template<>
static array<ref_t_constraint(Guid)^>^ convert(const std::vector<char>& native)
{
return gcnew array<Guid^>(1);
}
私はエラー てしまった "エラーC2912:明示的な特殊 'CLI ::配列^バズ::変換(のconstのstd ::ベクトル> &)' は関数テンプレートの専門ではありません"。
未使用の関数パラメータによる制約で、別のエラーが発生する可能性があります。テンプレート関数の特殊化では、デフォルトのパラメータを使用できません。 追加のテンプレート引数による制約が機能しません。 VC++ 120でのSFINAE実装のためだと思います。
このようなソリューションを実装することは可能ですか?多分私は何か間違っているのですか? VC++ 120を使用しています。
「制約」ではなく「制約」については確かですか?それは問題ではありませんが、英語の話者は混乱するかもしれません。 – Walter
値を扱い、別々に参照する場合は、それぞれの型特性を使用しないのですが、なぜ完全に無関係なもの(つまり 'is_integral'と' is_floating_point')を使用していませんか? – Walter
@Walter「値の型と参照の型を別々に扱う」と思ったとき、int型、float型、bool型などの組み込みの単純な型の配列が必要な場合は、C++ \ CLIで次のように作成する必要があります。gcnew array()に '^'を付けずに配列( '^'付き)を使用して他の型(参照型)を作成する必要がありました –