SFINAE経由でテンプレートテンプレートパラメータにバインドできるタイプがあるかどうかをテストできますか?SFINAEを使用して、型がテンプレートテンプレートパラメータにバインドできるかどうかを確認します。
私は私は何をしようとすると、最良の次のコード例で説明されていると思う:
#include <iostream>
template<typename... T> using void_t = void;
template<typename T> struct TemporaryBindObject
{
using type = TemporaryBindObject<T>;
};
template<template<typename...> class Dest> struct TestValidBind
{
template<typename... Ts> struct toTypesOf
{
using type = std::false_type;
};
template<template<typename...> class Src, typename... Ts> struct toTypesOf<Src<Ts...>, void_t<Dest<Ts...,float>>>
{
using type = std::true_type;
};
};
template<typename T> struct OneParamStruct{};
template<typename T1, typename T2> struct TwoParamStruct{};
int main()
{
using tmp = TemporaryBindObject<int>;
std::cout << "Can bind to TwoParamStruct: " << TestValidBind<TwoParamStruct>::toTypesOf<tmp>::type::value << std::endl;
std::cout << "Can bind to OneParamStruct: " << TestValidBind<OneParamStruct>::toTypesOf<tmp>::type::value << std::endl;
}
まず、私は別のクラスにバインドするテンプレートパラメータint
を取るしたいから、一時的なタイプtmp
を作りますテンプレート。 TestValidBind<template template type>::toTypesOf<typename>
template template parameter
とに所定のタイプのパラメータをバインドし、追加のタイプ(この例ではfloat
)を追加できるかどうかをテストします。
私が欲しいのは、TestValidBind<TwoParamStruct>::toTypesOf<tmp>::type
がtrue_type
であり、TestValidBind<OneParamStruct>::toTypesOf<tmp>::type
がfalse_type
であることです。次のエラーで
がg++ -std=c++11
(5.3.1)でコンパイルしないであるようにコード例:
../test_SFINAE_with_template_binding.cc: In function ‘int main()’: ../test_SFINAE_with_template_binding.cc:34:96: error: ‘TestValidBind<OneParamStruct>::toTypesOf<TemporaryBindObject<int> >::type’ has not been declared
とOneParamStruct
行が削除された場合レポートfalse_type
(間違っています)。
clang++ -std=c++11
(3.8.0)の場合、コードはコンパイルされますが、どちらの場合もfalse_type
と報告されます。
これはまったく同じですか?
編集:は、私は、追加のタイプが可能であるかどうかを確認したいことを強調するためにfloat
にvoid
から追加の種類を変更しました。
有効な修正をいただきありがとうございます。実際には、私の質問に正確に答えていません。私はまだ動作しません(おそらく私の例は、ポイントを強調するために適切に調整されていない)の反対を求めていた。私が試したかったのは、追加のタイプを追加することでした(私は「無効」にしました)。私はこの点を強調するために何かに変更します。 – havogt
私のコメントでは少し速すぎました:私の例(タイプが多すぎます)があなたの修正で今clangで動作しますが、まだg ++ 5で失敗します。あなたの例は両方のコンパイラで動作します。 – havogt
私はあなたの答えに合うように私の質問を調整します(本当にあなたは愚かな間違いを修正したので)、明日新しい質問をします。しかし、私は編集で私の質問を変更していないことに注意しよう! – havogt