鑑於此模板:使用enable_if根據需要添加結構成員
template <class A>
struct Something {
... // members common to all template instantiations for all A types
SpecialType member; // but not this - I want this to be conditional...
}
...我想用「enable_if」有SpecialType成員有條件地存在;也就是說,只有當模板使用A = SpecialCase1或SpecialCase2類型實例化時。在其他所有情況下,我想讓SpecialType成員失蹤。
如果你想知道爲什麼,這是關於優化 - 即不在結構中攜帶無用的有效載荷。我是一個模板元編程的新手,但我知道我需要「enable_if」和兩個「is_same」 - 不知道如何確切如何,雖然...
編輯:這與通用C + +(即沒有提升細節)會是一個優點。
'if_'通常被稱爲'std :: conditional'。 – 2012-04-13 12:12:45
@KerrekSB:啊,謝謝,我恐怕有點老前輩。在Boost MPL中它是'if_' :)我還沒有在C++ 11新庫中挖掘太多:/ – 2012-04-13 12:55:16