- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我很确定这个问题的答案是,“模板永远不可能成为复制构造函数。”
不幸的是,我只花了 3 个小时弄清楚为什么我会收到有关递归的警告,跟踪它到复制构造函数,看着调试器发疯,不让我看递归代码,最后跟踪到一个基础构造函数中缺少“&”。
你看,我有一个复杂的基于策略的设计主机,它已经运行了一段时间了。我着手将两个策略合二为一并遇到了一个递归复制构造函数。将其缩小为一个策略,该策略需要提供一个构造函数,该构造函数可以采用一种 XXX 概念作为其参数,但在这种情况下,我只是放弃它。所以我写了
struct my_policy
{
template < typename T >
my_polity(T const) {} // missing '&'...oops
};
现在,my_policy 是主机的基类(当然),这个小错字导致递归,其中主机的复制构造函数将自身向上传递到这个模板化构造函数,而不是编译器生成的隐式复制构造函数。然后它当然会再次调用它的复制构造函数来创建临时文件。
真正令人着迷的是我无法用简化的代码重新创建它。即使有一种模拟策略主机示例,我也无法实现它。以下代码未出现此问题:
#include <boost/utility/enable_if.hpp>
#include <boost/mpl/bool.hpp>
struct base
{
template < typename T >
base(T const) {}
};
struct another_base
{
int x;
another_base(int y) : x(y) {}
};
template < typename T >
struct is_derived : boost::mpl::false_ {};
template < typename T1, typename T2 >
struct derived : T1, T2
{
template < typename T >
derived(T const& x, typename boost::disable_if< is_derived<T> >::type * = 0) : T1(0), T2(x) {}
};
template < typename T1, typename T2 >
struct is_derived<derived<T1,T2>> : boost::mpl::true_ {};
int main()
{
derived<base, another_base> d(23);
derived<base, another_base> x = d;
}
我正在使用 boost 的参数库使主机的 7 个左右的参数可以通过“名称”访问。也许这就是问题所在,我不知道。无论如何,我想知道是否有人知道什么特定条件(如果有的话)可能导致编译器合法地将模板化构造函数用于“base”作为复制构造函数或从隐式复制构造函数用于“derived”。
编辑备注:
我通过给“another_base”一个显式复制构造函数来重现上面代码中的问题:
struct another_base
{
int x;
another_base(another_base const& b) : x(b.x) {}
another_base(int y) : x(y) {}
};
开始得出结论,这是一个编译器错误,除非有人能告诉我为什么这是合法的。
更多信息:
struct derived;
struct base
{
base() {}
private:
base(derived const&);
};
struct base2
{
base2() {}
//base2 (base2 const&) {}
};
struct derived : base, base2 {};
int main()
{
derived d1; derived d2(d1);
}
仔细查看 Schaub 的回答,我采用了上面的代码并进行了编译。在您取消注释 base2 的复制构造函数声明之前,它编译得很好。然后它将以我假设的原始代码预期的方式爆炸(无法访问 base 中的私有(private)构造函数)。所以模板甚至不是问题的一部分;您可以在没有它们的情况下重现问题。看起来这是一个 MI 问题,VS 在正确处理这个问题上总是有点慢。
我已经更改了标签以反射(reflect)这一发现。
发布到 MS 的错误存储库
我在示例代码中加入了变通方法。
最佳答案
对于 Visual C++,复制构造函数参数的派生到基委派存在一个问题:
struct Derived;
struct Base {
Base(){ }
private:
Base(Derived const&);
};
struct Derived : Base { };
Derived d1;
Derived d2(d1);
此代码有效,但 Visual C++ 无法编译它,因为它们使用 Derived
对象调用基类复制构造函数。然而,该标准要求编译器将 Base const
(或在某些情况下为 Base
)传递给基类。
这是您的难题的第一部分:模板更匹配,因为复制构造函数需要派生到基类的转换,但您的模板直接接受派生类,然后需要另一个拷贝,依此类推等等。请注意,这里的模板不会充当复制构造函数(考虑到 VC++ 错误),就像上面的 Base(Derived const&)
声明没有声明复制构造函数一样.
第二部分是对您的另一个问题的回答:对于实例化模板是否可以充当复制构造函数,标准在 C++03 中含糊不清且不清楚。在一张纸条上,它说
Because a template constructor is never a copy constructor, the presence of such a template does not suppress the implicit declaration of a copy constructor. Template constructors participate in overload resolution with other constructors, including copy constructors, and a template constructor may be used to copy an object if it provides a better match than other constructors.
但是下面的几段,它说
A member function template is never instantiated to perform the copy of a class object to an object of its class type.
由于本文出现的上下文(禁止按值参数复制构造函数),有人可能会争辩说这并不禁止从模板实例化按引用复制构造函数。但面对这种模糊的措辞,这样的争论是没有意义的。
C++0x FCD 清除了它,并删除了奇怪的注释。现在很清楚,模板永远不会被实例化来执行复制,无论它是否会屈服于按引用或按值参数。但是正如上面所解释的,如果您碰巧使用了 VC++,并且它表现出这种行为,那么它与复制构造函数无关。
关于c++ - MI 和隐式复制构造函数错误(原为 : Under what conditions can a template be the copy constructor?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3527248/
谁能解释一下原因: (define a (lambda() (cons a #f))) (car (a)) ==> procedure ((car (a))) ==> (procedure . #f)
这是 PyBrain 网站的摘录。我了解大部分正在发生的事情,但是一行让我完全难住了。我以前从未在 python 代码中看到过这样的东西。这是整个循环,对于上下文: for c in [0,
我是gradle / groovy的新手。我想创建将做一些事情的自定义任务。我的第一个问题是任务完成时该如何做?我可以覆盖doFirst / doLast闭包吗?也许我可以重写某些在开始和结束时都会执
我刚刚开始评估 MS 企业库。他们使用以下指令来获取实例: var customerDb = EnterpriseLibraryContainer.Current.GetInstance("C
这是我的 if else Ansible 逻辑.. - name: Check certs exist stat: path=/etc/letsencrypt/live/{{ rootDomain
我正在使用construct 2.8 对一些失传已久的 Pascal 程序创建的一些文件的 header 进行逆向工程。 header 由许多不同的记录组成,其中一些是可选的,我不确定顺序是否固定。
我在将 getchar() 的输入放入 char *arr[] 数组时遇到问题。我这样做的原因是因为输入数据(将是一个带有命令行参数的文件)将存储在一个 char 指针数组中以传递给 execvp 函
通常我们不能约束类型参数 T派生自密封类型(例如 struct 类型)。这将毫无意义,因为只有一种类型适合,因此不需要泛型。所以约束如下: where T : string 或: where T :
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 9 年前。 Improve th
#include using namespace std; class A { private: int m_i; friend int main(int argc, char cons
这个问题在这里已经有了答案: Are there legitimate uses for JavaScript's "with" statement? (33 个答案) 关闭 9 年前。 我有这个代
在this answer我看到了下一个 Bash 结构。 yes "$(< file.txt)" 什么意思 "$(< file.txt)" ? 我明白了 命令替换 - $(command)用命令的结
if (a == 1) //do something else if (a == 2) //do something else if (a == 3) //do somethi
关于构造的快速简单的问题。 我有以下用于将项目添加到 ListView 的代码。 ListViewItem item = new ListViewItem(); item.Text = file; i
我想使用 std::vector 来控制给定的内存。首先,我很确定这不是好的做法,但好奇心占了上风,无论如何我都想知道如何做到这一点。 我遇到的问题是这样的方法: vector getRow(unsi
下面显示了一段简单的javascript: var mystring = ("random","ignored","text","h") + ("ello world") 这个字符串会生成 hello
在 Java 中,创建对象的标准方法是使用 MyClass name = new MyClass(); 我也经常看到构造 new MyClass() { /*stuff goes in here*/
我正在编写 C++ ndarray 类。我需要动态大小和编译时大小已知的数组(分别分配自由存储和分配堆栈)。我想支持从嵌套的 std::initializer_list 进行初始化。 动态大小的没问题
我正在将一个项目从 Visual Studio 2005 转换为 Visual Studio 2008,并提出了上述结构。 using Castle.Core.Resource; using Cast
我想知道我在这里的想法是否正确,我主要针对接口(interface)进行编程,所以我想知道下面的类是否应该通过 DI 注入(inject),或者我应该自己实例化一个类... 注意:这些服务保存在我的核
我是一名优秀的程序员,十分优秀!