相关疑难解决方法(0)

数组维度中的C++模板参数

我有以下代码使用模板和数组维度作为模板非类型参数

template<int n> double f(double c[n]);
...
double c[5];
f<5>(c);  // compiles
f(c);  // does not compile
Run Code Online (Sandbox Code Playgroud)

编译器是否应该能够在没有显式模板参数的情况下实例化第二个f?我正在使用g ++ 4.1

c++ templates

8
推荐指数
1
解决办法
9487
查看次数

在std :: array上使用std :: extent

我有一个模板化的功能,我想static_assert它的类型有三个大小.此代码说明了我正在尝试做什么,但不起作用:

template < typename T >
void foo( T& param )
{
    // This line is the one that I need to figure out how to write
    static_assert( 3 == std::extent< T >::value, "param must have a size of 3" );
}

int main( void )
{
    int cArray[3];
    std::array< int, 3 > stdArray;

    foo( cArray );
    foo( stdArray );
}
Run Code Online (Sandbox Code Playgroud)

c++ arrays templates static-assert

5
推荐指数
2
解决办法
1582
查看次数

标签 统计

c++ ×2

templates ×2

arrays ×1

static-assert ×1