簡體   English   中英

在可變參數模板參數之后,C ++是否允許正常參數?

[英]Does C++ allow normal parameters after variadic template parameters?

根據cppreference ,以下代碼是合法的:

lock_guard( MutexTypes&... m, std::adopt_lock_t t );

但是,以下代碼無法使用clang 3.8(-std = c ++ 1z)進行編譯:

template<typename... Args>
void f(Args&&..., bool)
{}

int main()
{
    f(1, 2, 3, true); // error! see below for details.
}
 1>main.cpp(59,2): error : no matching function for call to 'f' 1> f(1, 2, 3, true); 1> ^ 1> main.cpp(54,6) : note: candidate function not viable: requires 1 argument, but 4 were provided 1> void f(Args&&..., bool) 1> ^ 1> 1 error generated. 

在可變參數之后,C ++是否允許正常參數?

代碼中的函數聲明是有效的,但是對於此類函數模板, 演繹不起作用。 請注意,以下代碼格式正確,並實例化了特殊化void f(int, int, int, bool)

template<typename... Args>
void f(Args&&..., bool) {}

int main() {
    f<int, int, int>(1, 2, 3, true);
}

請注意,在C ++ 17中, MutexTypes...是類本身的模板參數:

template <class... MutexTypes> class lock_guard;

所以他們是眾所周知的,不需要推斷。 請注意,帶有adopt_lock_t的構造函數不能用於C ++ 17類模板參數推導,因為adopt_lock_t參數出現在參數包之后。 如果委員會在C ++ 11中具有先見之明,那么他們就會把adopt_lock_t論點放在開頭而不是最后,但唉,現在已經太晚了。

暫無
暫無

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

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