[英]C++ ::std::enable_if and virtual
我掃描了答案,但是...它們似乎是愚蠢的問題。
我非常滿意並且完全理解(我對此不太強調)為什么擁有類的模板虛擬成員毫無意義。
考慮抽象基類List
(具有繼承自LinkedList
和ArrayList
List
)
如果列表存儲的類型具有標識概念(不是字符串或整數,則沒有任何明智的“ ==”,我不會在此處顯示POD),則需要一種方法
virtual bool contains(T& what) const;
和
virtual int index(T& what) const;
但是,如果它是一種沒有身份的類型,例如字符串或數字,則需要:
virtual int countOccurrences(T& what) const;
和
virtual int find(T& what, int occurrence=0) const;
說。
無法使用::std::enable_if
完成此操作,因此您必須執行以下操作:
template<class T, bool HAS_IDENTITY> class List;
template<class T> class List<false> {
virtual int countOccurrences(T& what) const=0;
virtual int find(T& what, int occurrence=0) const=0;
/*other stuff*/
};
template<class T> class List<true> {
virtual bool contains(T& what) const =0;
virtual int index(T& what) const =0;
/*other stuff*/
};
這不是很糟糕,但是有很多代碼重復,而且我只會在必須時弄濕(反對DRY)。
如果我將通用代碼隱藏在基類中,那就更好了。
我的問題涉及使用這種方法進行縮放,這里我們有一個布爾,提供2個專業,假設我有n個布爾,那么有2 ^ n個專業,我看不到我需要超過4個的情況。仍然有16個班級參與! 8比3,不是很好。
假設我有一個枚舉和一個布爾值,那么我有2個枚舉計數專業。
它增長很快。
以前,我們使用宏定義類,並且使用類名中的##
運算符從本質上像模板那樣處理它。 我必須說,雖然我現在很喜歡enable_if
和朋友...
有人可以告訴我解決此問題的模式嗎?
只是一個q&d hack,但這應該提供一些提示。
我不知何故甚至可以擺脫那種丑陋的“虛擬”參數,但是我現在看不到
編輯7月6日
我使ansatz使用起來更加無縫。 對“身份”概念的編譯時測試(即問題開放者顯然針對的目標)將要求對
//T t1, t2;
(t1 == t2) == (&t1 == &t2);
這是不可能的。 因此,我介紹了功能列表的概念,以方便手動分配這些功能。
#include <iostream>
#include <typeinfo>
#include <type_traits>
#ifdef __GNUG__
#include <cxxabi.h>
auto type_str = [](const std::type_info& ti) {
int stat;
return abi::__cxa_demangle(ti.name(), 0, 0, &stat);
};
#else
#warning untested
auto type_str = [](const std::type_info& ti) {
return ti.name();
};
#endif
typedef int Feature;
const Feature HAS_IDENTITY = 1;
const Feature HAS_FOOBAR = 2;
const Feature HAS_NO_IDENTITY = -HAS_IDENTITY;
const Feature HAS_NO_FOOBAR = -HAS_FOOBAR;
const Feature _TERM_ = 0;
template<typename T, Feature F>
struct has_feature : std::false_type {};
template<int N , int M>
struct is_greater {
constexpr static bool value = N > M;
};
namespace detail {
template<class T, Feature... Fs> struct List {}; // primary template
template<class T, Feature F>
struct List<T,F> {};
template<class T, Feature F, Feature... Fs>
struct List<T,F,Fs...>
: virtual public
std::conditional<
has_feature<T,F>::value,
List<T, F>,
List<T, -F>
>::type,
virtual public
std::conditional<
is_greater<sizeof...(Fs),0>::value,
List<T, Fs...>,
List<T, _TERM_>
> ::type {};
template<class T> struct List<T, _TERM_> {};
template<class T>
struct List<T,HAS_NO_FOOBAR> {
virtual std::string hello() const /* = 0;*/ {
return std::string("\"What the foo is FOOBAR?\", askes ") + type_str(typeid(T));
}
};
template<class T>
struct List<T,HAS_FOOBAR> {
virtual std::string hello() const /* = 0;*/ {
return std::string("\"For sure I'm FOOBAR\", says ") + type_str(typeid(T));
}
};
template<class T>
struct List<T,HAS_NO_IDENTITY> {
virtual int index(const T& what) const /* = 0;*/ {
return 137;
}
};
template<class T>
struct List<T,HAS_IDENTITY> {
virtual int index(const T& what) const /* = 0;*/ {
return 42;
}
};
template<typename T>
using Feature_Aware_List = List<T,HAS_IDENTITY,HAS_FOOBAR, /* all Features incuding*/_TERM_>;
} //namespace detail
template<typename T>
using List = detail::Feature_Aware_List<T>;
struct Gadget {
bool operator== (const Gadget& rhs) const {
return this == &rhs;
}
};
struct Gimmick {
bool operator== (const Gimmick& rhs) const {
return this == &rhs;
}
};
template<Feature F>
struct FeatureList {};
template<>
struct FeatureList<HAS_IDENTITY>
: public Gadget,
public Gimmick
/**/
{};
#include <valarray>
template<>
struct FeatureList<HAS_FOOBAR>
: public std::valarray<float>
/**/
{};
template<class T>
struct has_feature<T, HAS_IDENTITY>
: public std::conditional<
std::is_base_of<T, FeatureList<HAS_IDENTITY>>::value,
std::true_type,
std::false_type
>::type {};
template<class T>
struct has_feature<T, HAS_FOOBAR>
: public std::conditional<
std::is_base_of<T, FeatureList<HAS_FOOBAR>>::value,
std::true_type,
std::false_type
>::type {};
int main() {
List<Gadget> l1 ;
List<std::valarray<float>> l2;
std::cout << l1.hello() << " #" << l1.index(Gadget()) << std::endl;
std::cout << l2.hello() << " #" << l2.index(std::valarray<float>()) << std::endl;
}
輸出:
"What the foo is FOOBAR?", askes Gadget #42
"For sure I'm FOOBAR", says std::valarray<float> #137
不言而喻,沒有實現任何特定的“列表”功能,這僅是模擬的
您可以使用模板策略:
template<class T, bool HAS_IDENTITY> class ListIdentityPolicy;
template<class T> class ListIdentityPolicy<T, false> {
virtual int countOccurrences(T& what) const = 0;
virtual int find(T& what, int occurrence = 0) const = 0;
};
template<class T> class ListIdentityPolicy<T, true> {
virtual bool contains(T& what) const = 0;
virtual int index(T& what) const = 0;
};
template<class T, bool HAS_FOOBAR> struct ListFoobarPolicy;
template<class T> struct ListFoobarPolicy<T, false> {
virtual void foo() = 0;
};
template<class T> struct ListFoobarPolicy<T, true> {
virtual void bar() = 0;
};
template <class T> class List
: public ListIdentityPolicy<T, HasIdentity<T>::value>
, public ListFoobarPolicy<T, HasFoobar<T>::value>
{
public:
/*other stuff*/
};
HasIdentity
和HasFoobar
是要定義的類型特征,每個特征都包含一個static const bool value
該static const bool value
指示T
是否具有相應的屬性。
或者,您可以為List
一個非虛擬的公共API,並在實現中隱藏動態調度:
template <class T> class List
{
public:
enum Impl {
LinkedList = 0,
ArrayList,
};
List(Impl i) : pimpl(makePimpl(i)) {}
List(List const& other) : pimpl(other.pimpl->clone())
List& operator=(List const& other) { pimpl = other.pimpl->clone(); }
int count(T& what) const
{ static_assert(! HasIdentity<T>::value, "oops"); return pimpl->count(what); }
int find(T& what, int n = 0) const
{ static_assert(! HasIdentity<T>::value, "oops"); return pimpl->find(what, n); }
bool contains(T& what) const
{ static_assert(HasIdentity<T>::value, "oops"); return pimpl->contains(what); }
int index(T& what) const
{ static_assert(HasIdentity<T>::value, "oops"); return pimpl->index(what); }
void foo()
{ static_assert(! HasFoobar<T>::value, "oops"); pimpl->foo(); }
void bar()
{ static_assert(HasFoobar<T>::value, "oops"); pimpl->bar(); }
private:
struct AbstractPimpl
{
virtual std::unique_ptr<AbstractPimpl> clone() const = 0;
virtual int count(T& what) const = 0;
virtual int find(T& what, int n = 0) const = 0;
virtual bool contains(T& what) const = 0;
virtual int index(T& what) const = 0;
virtual void foo() = 0;
virtual void bar() = 0;
};
struct LinkedListPimpl : public AbstractPimpl
{
std::unique_ptr<AbstractPimpl> clone() override;
int count(T& what) const override;
int find(T& what, int n = 0) const override;
bool contains(T& what) const override;
int index(T& what) const override;
void foo() override;
void bar() override;
/* ... */
};
struct ArrayListPimpl : public AbstractPimpl
{
std::unique_ptr<AbstractPimpl> clone() override;
virtual int count(T& what) const override;
virtual int find(T& what, int n = 0) const override;
virtual bool contains(T& what) const override;
virtual int index(T& what) const override;
virtual void foo() override;
virtual void bar() override;
/* ... */
};
std::unique_ptr<AbstractPimpl> pimpl;
static std::unique_ptr<AbstractPimpl> makePimpl(Impl i) {
switch (i) {
LinkedList: default:
return std::make_unique<LinkedListPimpl>();
ArrayList:
return std::make_unique<ArrayListPimpl>();
}
}
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.