Nadro:
私は、次の問題への解決策を探しています:
#include <string>
class A
{
public:
template <typename T>
static typename std::enable_if<std::is_same<T, std::string>::value, void>::type foo(T val)
{
printf("std::string\n");
}
template<typename T, typename... Arg>
static void multiple(Arg&&... arg)
{
T::foo(arg...);
}
};
class B : public A
{
public:
template <typename T>
static typename std::enable_if<std::is_same<T, int>::value, void>::type foo(T val)
{
printf("int\n");
}
};
int main()
{
std::string a;
int b = 0;
A::multiple<B>(a, b);
}
両方の場合は、すべてが正常に動作しますfoo
方法は、同じクラスまたはI力であるfoo
適切なクラスから(A::foo
のためにstd::string
とB::foo
のためのint
ベースクラスは拡張可能でなければならないので)、しかし私は、複数のクラスを必要としています。私はより多くのSFINAEがために検出などの機能必要があるので、私は、簡単な特殊化を使用することはできませんstd::pair
、std::tuple
など私も名前空間にクラスからのfooメソッドを移動する必要はありません。あなたはどのように私はこの問題を解決することができます任意のアイデアを持っていますか?
Jarod42:
ここではB::foo
皮革A::foo
、次のものが必要ですusing
:
class B : public A
{
public:
using A::foo;
template <typename T>
static typename std::enable_if<std::is_same<T, int>::value, void>::type foo(T val)
{
printf("int\n");
}
};
だが
namespace.udecl番号15.sentence-1 :
派生クラスのオーバーライドおよび/または非メンバ関数と同じ名前を持つメンバ関数テンプレートは、パラメータ型リスト、CV-で使用-宣言子は派生クラスに基本クラスからの宣言をもたらす、メンバ関数とメンバ関数テンプレート資格、ベースクラス(よりむしろ競合)のRef-修飾子(もしあれば)
戻り値の型はカウントされませんので、あなたが使用する必要がありstd::enable_if
、パラメータに:
class A
{
public:
template <typename T>
static void foo(T val, std::enable_if_t<std::is_same<T, std::string>::value, int> = 0)
{
printf("std::string\n");
}
};
class B : public A
{
public:
using A::foo;
template <typename T>
static void foo(T val, std::enable_if_t<std::is_same<T, int>::value, int> = 0)
{
printf("int\n");
}
};
注:あなたはまたのためにタイプミスがあります
template<typename T, typename... Arg>
static void multiple(Arg&&... arg)
{
T::foo(arg...); // B::foo(string, int)
}
これでなければなりません
template<typename T, typename... Arg>
static void multiple(Arg&&... arg)
{
(T::foo(arg), ...); // B::foo(string), B::foo(int)
}