[英]c++11: delegate constructor - can't select constructor template?
这是一个后续问题:
c ++ 11专用的“代理构造函数”委托给私有通用引用构造函数?
我想摆脱那里使用的“枚举类虚拟”。
但我无法委托给模板构造函数。
请参阅下面的代码示例。
#include <iostream>
#include <string>
#include <typeinfo>
class MyClass
{
private:
template <class T>
MyClass(T&& data)
: _data(std::forward<T>(data))
{
std::cout << "MyClass universal reference template c'tor" << std::endl;
}
public:
// proxy c'tors delegating to universal reference c'tor
MyClass (std::string const & data)
: MyClass<std::string>(data)
{
std::cout << "MyClass lvalue c'tor" << std::endl;
}
MyClass (std::string && data)
: MyClass<std::string>(std::move(data))
{
std::cout << "MyClass rvalue c'tor" << std::endl;
}
private:
std::string _data;
};
int
main(
int,
char**)
{
{
std::string str("demo");
MyClass myClass(str);
}
{
MyClass myClass("hello, world");
}
return 0;
}
我收到以下错误:
main2.cpp: In constructor 'MyClass::MyClass(const string&)':
main2.cpp:21:7: error: 'class MyClass MyClass::MyClass' is not a non-static data member of 'MyClass'
: MyClass<std::string>(data)
^
main2.cpp:21:14: error: expected '(' before '<' token
: MyClass<std::string>(data)
^
main2.cpp:21:14: error: expected '{' before '<' token
main2.cpp: In constructor 'MyClass::MyClass(std::__cxx11::string&&)':
main2.cpp:27:7: error: 'class MyClass MyClass::MyClass' is not a non-static data member of 'MyClass'
: MyClass<std::string>(std::move(data))
^
main2.cpp:27:14: error: expected '(' before '<' token
: MyClass<std::string>(std::move(data))
^
main2.cpp:27:14: error: expected '{' before '<' token
main2.cpp: In function 'int main(int, char**)':
main2.cpp:11:5: error: 'MyClass::MyClass(T&&) [with T = std::__cxx11::basic_string<char>&]' is private
MyClass(T&& data)
^
main2.cpp:46:28: error: within this context
MyClass myClass(str);
^
main2.cpp:11:5: error: 'MyClass::MyClass(T&&) [with T = const char (&)[13]]' is private
MyClass(T&& data)
^
main2.cpp:50:39: error: within this context
MyClass myClass("hello, world");
这并不特定于委托构造函数。 构造函数(和转换函数)没有名称,因此语言中无法为它们提供显式模板参数。 引用 C++14,[temp.mem] 14.5.2/5:
[注意:因为显式模板实参列表跟在函数模板名之后,并且因为转换成员函数模板和构造函数成员函数模板是在不使用函数名的情况下调用的,所以无法为这些函数模板提供显式模板实参列表。 ——尾注]
注释不是规范性的,但本注释只是明确说明了第 14 章中的规则所遵循的内容。
Scott Mayers 无疑是一位 C++ 专家,但他并不总能做到正确。
问题:
允许最有效的构造,尽可能限制副本
回答:
X 值构造函数,受 std::enable_if 限制:
#include <iostream>
#include <string>
#include <type_traits>
class MyClass
{
public:
template <class T, std::enable_if_t<std::is_constructible<std::string, T>::value>* = nullptr>
MyClass(T&& data)
: _data(std::forward<T>(data))
{
std::cout << "MyClass universal reference template c'tor" << std::endl;
}
private:
std::string _data;
};
int main()
{
using namespace std::string_literals;
auto a = MyClass("hello"s);
auto b = MyClass("world");
const auto s = "Hello, World"s;
auto s2 = "Hello, World";
auto c = MyClass(s);
auto d = MyClass(s2);
// won't compile
// auto e = MyClass(10);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.