2016-11-15 119 views
3

在編譯以下代碼期間檢查糾正性(用於從類型列表中選擇類型的元函數,從右到左從1開始索引)在GCC上失敗,而接受此代碼:在類模板專業化中使用sizeof模板參數包

#include <cstdlib> 

template< std::size_t i, typename ...types > 
struct at_index 
{ 

}; 

template< typename first, typename ...rest > 
struct at_index< (1 + sizeof...(rest)), first, rest... > 
{ 
    using type = first; 
}; 

template< std::size_t i, typename first, typename ...rest > 
struct at_index< i, first, rest... > 
     : at_index< i, rest... > 
{ 

}; 

int main() 
{ 
} 

哪個編譯器是正確的?

GCC錯誤消息:

error: template argument '(1 + sizeof... (rest))' involves template parameter(s) 
struct at_index< (1 + sizeof...(rest)), first, rest... > 
     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 

我想從參數包在sizeof...運營商的類型不應該被解析成符號名的某些部分。因此,在這裏不應有名稱損壞問題。

這是一個clang擴展名允許上面的代碼?

+2

無論參數的順序如何,問題都會發生[示例](http://melpon.org/wandbox/permlink/4fmoBTqwQbRhe5wU) –

+0

解決方法是使用'std :: integral_constant'而不是非類型的模板參數[示例](http://melpon.org/wandbox/permlink/oEeClM4wgFXCXk4g)。不知道您的代碼是否有效,但... –

+0

@ W.F。它就像另一個間接層:像'std :: void_t'。 – Orient

回答

4

N4140 [temp.class.spec] /8.1:

一種部分專門的非類型參數表達不得涉及 部分特的除了當 參數表達式是一個簡單的模板參數標識符

這已被放寬core issue 1315。據推測,海灣合作委員會還沒有開始實施。