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擴展名允許上面的代碼?
無論參數的順序如何,問題都會發生[示例](http://melpon.org/wandbox/permlink/4fmoBTqwQbRhe5wU) –
解決方法是使用'std :: integral_constant'而不是非類型的模板參數[示例](http://melpon.org/wandbox/permlink/oEeClM4wgFXCXk4g)。不知道您的代碼是否有效,但... –
@ W.F。它就像另一個間接層:像'std :: void_t'。 – Orient