簡體   English   中英

使用專門的可變參數模板類的聲明

[英]using declaration of a specialized variadic template class

是否可以以這樣的方式定義FloatType ,即可以將f1聲明為

FloatType f1;

代替

FloatType<> f1;

如果我嘗試使用前者,我會得到一個

error: use of class template 'FloatType' requires template arguments

template <typename T, typename... Args>
class Type
{
};

template <typename... Args>
class Type<float, Args...>
{
};

template <typename... Args>
using FloatType = Type<float, Args...>;

int
main(int, char **)
{
    FloatType<> f1;
    FloatType<float> f2;

    return 0;
}

不,那是不可能的。 根據標准§14.3/ 4,請強調:

使用模板參數包或默認模板參數時模板參數列表可以為空。 在那種情況下,空的<>括號仍應用template-argument-list [示例:

  template <class T = char> class String; String<>* p; // OK: String<char> String* q; // syntax error template <class ... Elements> class Tuple; Tuple<>* t; // OK: Elements is empty Tuple* u; // syntax error 

—末例]

但是,編寫FloatType<>什么問題呢? 如果看到空括號確實讓您感到討厭,則可以為它們引入另一個別名,但這種方法會使您感到困惑:

using DefFloatType = FloatType<>;

此外,還有更多輸入內容!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM