- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
谁能解释为什么代码无法编译。
template<class T, class DER>
struct Base {
T a;
Base(const T argB) : a(argB){}
};
template<class T>
struct Derived : Base<T, Derived<T> > {
Derived(const T argD) : Base<T, Derived<T> >(argD){}
};
int main() {
int val = 10;
const int *p = &val;
/* this was in the original question
Derived<int*> d(p); // breaks, but compiles with Derived<const int*> d(p);
*/
Derived d(p); // fails, but Derived<const int*> d(p); compiles
}
错误信息是关于没有从int*
到const int*
的转换。正如我所见,T
可以被 int*
替代,在这种情况下,Derived
的构造函数接收其参数作为 const int *
并使用 const int*
调用基类。那么为什么不断的qulaification会丢失。
我显然不明白模板参数推导是如何工作的。当 const
、*
和 &
发挥作用时,我还没有找到任何关于它如何工作的清晰但严谨和详尽的描述。也就是说,在这些不同的情况下,a
将被推导出什么类型。
Foo(T& a)
Foo(T a)
Foo(T* a)
Foo(const T a)
Foo(const T*a)
Foo(const t&a)
当 a
是
最佳答案
因为 Derived
的构造函数是 Derived(const T argD)
,所以在你的情况下它是 Derived(int * const)
。这不接受 const int*
。
“const(指向 int 的指针)” 不是“(指向 const int 的指针)”。
关于c++ - 模板参数推导和 const 限定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13655465/
在 Haskell 中,我可以导入由其名称或快捷方式名称限定的模块,如下所示: import qualified Data.List as List import qualified Data.Map
我在头文件中有以下函数声明: extern void flash(const char *message, const enum msg_type type); 基本上,它有两个参数并将相应的消息推送
我遇到了一个我似乎无法理解的小问题。 我正在我的数据库中搜索一个用户一个月内收到的评论总数,然后绘制成图表。 我遇到的问题是在我的 SQL 语句中使用 MONTHNAME 如果我使用 $this->d
根据 this answer , 常量不应该被删除,因为它们可能一开始就没有分配。但是,在某些情况下我想保护动态分配的数据。例如,在管理用户 session 时†,我想确保像当前用户名这样的数据不会被
我正在处理一个大型 C++ 项目。我有一堆在项目的子集中使用的宏,但我不想将它们导出到任何地方,以避免污染不相关的代码。 现在,我的许多文件看起来像 #include // defines MACR
谁能解释为什么代码无法编译。 template struct Base { T a; Base(const T argB) : a(argB){} }; template struct Der
我正在阅读 C++-Primer(作者:Josée Lajoie 和 Stanley B. Lippman),这时我看到了有关顶级和低级 const 的部分。在一段中,它说当复制对象时,顶级 cons
我有一大块 CSS,我想将其“作用域”到特定的 HTML block 。我正在生成一个唯一的 ID,然后将其设置在 HTML block 上,然后想用相同的 ID 包装 CSS block ,以便这些
我正在尝试为接受三个参数的函数创建一个通用接口(interface)。填写第一个参数时,第二个参数的选项应该在第一个参数的范围内。第三个参数应该在第二个参数的范围内。 我当前的代码如下所示: type
似乎 C++11 和 C++14 对待纯右值的 cv 限定不同。 C++11 坚持自 C++98 以来一直存在的“经典”方法:根据 3.10/4 “非类纯右值始终具有 cv 非限定类型”。 C++14
这个问题在这里已经有了答案: Stripping all qualifiers from a function type (1 个回答) 关闭去年。 我想编写一个模板,它接受一个指向成员函数的指针(
有时在引用同一类(或基类)的其他实例成员的实例成员中阅读代码可能会造成混淆: public void MyMethod() { Where = did + AllTheseWeirdThing
因此,在使用 constexpr 时,MSVC (Visual Studio 2012) 在尝试使用这个简单的程序(包括省略)使用 constexpr 关键字限定我的函数时给了我一个错误: const
这是我的 html(在 Twig 模板中) {{folder.name}} 我正在尝试从“data-jstree”获取“type”的值。 我试过用 var node_id = ref.get_node
当我尝试编译以下函数时出现错误。 string& foo(){ return "Hello World"; } Error: 1 IntelliSense: a reference of type
我有以下情况: 一个函数创建一个字符串数组,然后将其传递给一堆其他函数。这些其他函数不应修改外部指针指向的指针或字符串本身,因此我将它们设为常量。 最小的例子: void function(const
我希望能够限定 XHTML 文档中的 KnockOutJS 属性。 这是我想做的: My name is:
我正在浏览 Programming with Objective-C Apple 提供的文档。 我正在尝试理解以下段落,但到目前为止,无法理解。 @protocol XYZPieChartViewDa
给定两个 cv-unqualified 非数组对象类型 T1和 T2 , 可以表达 true ? std::declval() : std::declval()曾经有过 cv 限定的数组或函数类型吗?
我是一名优秀的程序员,十分优秀!