假設我有一個庫函式int foo( const T& )
,可以使用某些特定容器作為引數進行操作:
std::vector<A> c1;
std::list<B>() c2;
auto a1 = foo(c1); // ok
auto a2 = foo(c2); // ok too
std::map<int, float> c3;
auto a3 = foo( c3 ); // this must fail
首先,我撰寫了一個特征類來定義允許的容器:
template <typename T> struct IsContainer : std::false_type { };
template <typename T,std::size_t N> struct IsContainer<std::array<T,N>> : std::true_type { };
template <typename... Ts> struct IsContainer<std::vector<Ts...>>: std::true_type { };
template <typename... Ts> struct IsContainer<std::list<Ts... >>: std::true_type { };
現在,“Sfinae”功能:
template<
typename U,
typename std::enable_if<IsContainer<U>::value, U>::type* = nullptr
>
auto foo( const U& data )
{
// ... call some other function
return bar(data);
}
作業正常。
旁注:bar()實際上是在私有命名空間內,也可以從其他代碼中呼叫。相反,foo()是API的一部分。
但現在我想根據容器內的型別改變行為,即里面foo():
bar1(data)如果引數是std::vector<A>orstd::list<A>and則呼叫bar2(data)如果引數是std::vector<B>或則呼叫std::list<B>
所以我有這個:
struct A {}; // dummy here, but concrete type in my case
struct B {};
template<
typename U,
typename std::enable_if<
( IsContainer<U>::value, U>::type* = nullptr && std::is_same<U::value_type,A> )
>
auto foo( const U& data )
{
// ... call some other function
return bar1(data);
}
template<
typename U,
typename std::enable_if<
( IsContainer<U>::value, U>::type* = nullptr && std::is_same<U::value_type,B> )
>
auto foo( const U& data )
{
// ... call some other function
return bar2(data);
}
用bar1()and bar2()(dummy) 定義為:
template<typename T>
int bar1( const T& t )
{
return 42;
}
template<typename T>
int bar2( const T& t )
{
return 43;
}
這作業正常,如此處所示
現在我真正的問題:型別 A 和 B 實際上是由一些底層型別模板化的:
template<typename T>
struct A
{
T data;
}
template<typename T>
struct B
{
T data;
}
我希望能夠構建這個:
int main()
{
std::vector<A<int>> a;
std::list<B<float>> b;
std::cout << foo(a) << '\n'; // print 42
std::cout << foo(b) << '\n'; // print 43
}
我的問題是:我不知道如何“提取”包含的型別:我試過這個:
template<
typename U,
typename F,
typename std::enable_if<
( IsContainer<U>::value && std::is_same<typename U::value_type,A<F>>::value ),U
>::type* = nullptr
>
auto foo( const U& data )
{
return bar1(data);
}
template<
typename U,
typename F,
typename std::enable_if<
( IsContainer<U>::value && std::is_same<typename U::value_type,B<F>>::value ), U
>::type* = nullptr
>
auto foo( const U& data )
{
return bar2(data);
}
但這無法建立:
template argument deduction/substitution failed:
main.cpp:63:21: note: couldn't deduce template parameter 'F'
(見現場直播)
問:我怎樣才能使這項作業?
旁注:如果可能,請僅使用 C 14(或 17),目前我寧愿避免使用 C 20。
uj5u.com熱心網友回復:
有一個簡單的解決方案:以與定義IsA完全相同的方式定義特征IsContainer:
template<class> struct IsA : std::false_type {};
template<class T> struct IsA<A<T>> : std::true_type {};
然后寫
IsContainer<U>::value && IsA<typename U::value_type>::value
根據您的確切用例,您甚至可能不需要第一個特征,因為 for U = std::map<...>,無論如何IsA<typename U::value_type>::value都是。false
uj5u.com熱心網友回復:
isA您可以為/定義特征isB:
template <typename T> struct IsA : std::false_type { };
template <typename T> struct IsA<A<T>> : std::true_type { };
template <typename T> struct IsB : std::false_type { };
template <typename T> struct IsB<B<T>> : std::true_type { };
和基于這些特征的 SFINAE。
替代方案可能是標簽調度:
template<typename T> int bar1( const T& t ) { return 42; }
template<typename T> int bar2( const T& t ) { return 43; }
template <typename T> struct Tag{};
template<typename T, typename U>
int bar(const T& t, Tag<A<T>>) { return bar1(t); }
template<typename T, typename U>
int bar(const T& t, Tag<B<U>>) { return bar2(t); }
template<
typename T,
typename std::enable_if<IsContainer<T>::value>::type* = nullptr
>
auto foo( const U& data ) -> decltype(bar(data, Tag<typename T::value_type>{}))
{
// ... call some other function
return bar(data, Tag<typename T::value_type>{});
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/531470.html
