【发布时间】:2016-09-12 20:19:53
【问题描述】:
我不明白为什么编译器选择我的Production 类的复制构造函数并且没有其他候选函数。
我做了一个最小的例子来演示错误:
#include <string>
#include <typeindex>
#include <iostream>
struct DummyProduction {
};
struct Dep {
};
struct Pro {
};
class ModuleBase {
};
template<typename Production = DummyProduction>
class Provider {
public:
template<typename... Dependencies>
Provider(ModuleBase& module, Dependencies... args)
{
std::cout << "Provider called!" << std::endl;
}
Provider(const Provider&) = delete;
};
class TargetController : public ModuleBase,
public Provider<Pro>,
public Provider<>
{
public:
TargetController();
private:
Dep p;
};
TargetController::TargetController() :
ModuleBase(),
Provider<Pro>(*this, &p),
Provider<>(*this),
p()
{
}
int main()
{
TargetController x;
return 0;
}
我用 gcc 和 clang 试过了。以下是非工作示例的链接:link。
对于Provider<Pro>(*this, p),调用了正确的构造函数。但是对于第二个示例Provider<>(*this),编译器会尝试调用复制构造函数。
根据我从Overload resolution 页面的理解,所有匹配表达式的函数都应该进入候选函数集。但是,可变参数构造函数不在没有依赖关系的 Provider 的集合中,或者编译器选择了复制构造函数,尽管被删除了。
有没有办法避免这种行为?
【问题讨论】:
-
这样的问题让我很谦虚:)。为它 +1
-
[OT]:请注意,
p尚未在调用Provider<Pro>(*this, p)中构造。 -
@Jarod42 是的,没错,我在将代码简化为最小示例时忘记了这一点。
标签: c++ c++11 constructor c++14 variadic-templates