1
假設下面的類:從直接多重繼承變爲間接多重繼承時會出現什麼問題?
struct X : A, B, C{};
,如果我將其更改爲以下可能會出現什麼問題?
struct indirect_C : C{};
struct indirect_BC : B, indirect_C{};
struct X : A, indirect_BC{};
的例子似乎做作,但它發生在你繼承通過可變參數模板可變數量的基地,同時也希望從這些基地的功能在派生類中提供(或沒有)。
template<class... List>
struct X : List...{
using List::something...; // ill-formed in C++11
};
因此,你需要一個變通辦法,這是繼承「遞歸」,並且使功能集成到範圍在每一個遞歸步驟:
template<class Head, class... Tail>
struct inherit_indirect
: Head
, inherit_indirect<Tail...>
{
using Head::something;
using inherit_indirect<Tail...>::something;
};
template<class T>
struct inherit_indirect<T>
: T
{
using T::something;
};
(見例如this answer of mine,我曾經這個技巧)
你能描述一下用例嗎?委員會將歡迎使用「使用T :: x ...」的用例列表。 –
@Johannes:我知道有一種擴展解包機制的運動,像'使用T :: T ...;'或'使用T :: x ...;'在後面的標準中是可能的。這個特別的問題又出現了[在這個線程中](http://isocpp.org/forums/iso-c-standard-discussion?place=msg%2Fstd-discussion%2Fnd0OFYBxvP0%2FlKLCp7TQgbYJ),並且也可以通過提升一些限制開箱。我沒有記住任何特定的用例,這些用例不會被修復,我只是有興趣,如果有任何問題可能出現。 – Xeo