小编use*_*212的帖子

模板参数中的派生类类型无法编译

这是我无可救药地坚持的代码片段.

template <class T, T nt>
class C;

struct base{
   int i;
} b;

struct derived : base{} d;

C<base*,&d> obj;
Run Code Online (Sandbox Code Playgroud)

为什么这会给出错误could not convert template argument &d to base*

c++ templates

5
推荐指数
1
解决办法
773
查看次数

什么是const关键字是必要的

我的代码没有编译.

int foobar()
{
    // code
    return 5;
}

int main()
{
   int &p = foobar(); // error
   // code

   const int& x = foobar(); //compiles
}
Run Code Online (Sandbox Code Playgroud)

为什么添加关键字const会使代码编译?

c++ const reference

4
推荐指数
1
解决办法
186
查看次数

标签 统计

c++ ×2

const ×1

reference ×1

templates ×1