我试图使用使用SFINAE进行递归的递归静态成员函数从模板结构中的包中剥离参数。我知道我可以使用结构的部分专业化,但是我想尝试这种方式。这是测试代码
#include<type_traits>
template< int NDIM, typename INDEX, typename... REMAINING_INDICES >
struct helper
{
template< int DIM=0 >
inline static typename std::enable_if< DIM!=(NDIM-1), int>::type
f( int const * const strides,
INDEX index,
REMAINING_INDICES... indices )
{
return index*strides[0]
+ helper<NDIM, REMAINING_INDICES...>::f<DIM+1>( strides+1, indices... );
}
template< int DIM=0 >
inline static typename std::enable_if< DIM==(NDIM-1), int>::type
f( int const * const strides,
INDEX index )
{
return index;
}
};
int main()
{
}
gcc8给出:
g++ -std=c++14 main.cpp
main.cpp: In static member function 'static typename std::enable_if<(DIM != (NDIM - 1)), int>::type helper<NDIM, INDEX, REMAINING_INDICES>::f(const int*, INDEX, REMAINING_INDICES ...)':
main.cpp:41:80: error: expected binary operator before ')' token
+ helper<NDIM, REMAINING_INDICES...>::f<DIM+1>( strides+1, indices... );
^
clang6给出:
clang++ -std=c++14 main.cpp
main.cpp:41:76: error: expected ')'
+ helper<NDIM, REMAINING_INDICES...>::f<DIM+1>( strides+1, indices... );
^
main.cpp:41:56: note: to match this '('
+ helper<NDIM, REMAINING_INDICES...>::f<DIM+1>( strides+1, indices... );
^
main.cpp:40:12: error: expression contains unexpanded parameter pack 'indices'
return index*strides[0]
^
2 errors generated.
有人知道为什么这行不通吗?