我希望能够使用move semantics或复制语义来初始化类的每个字段。构造函数将使用基本相同的构造代码,如下所示:
LogRecord::LogRecord(const Logger &logger, LogLevel level, const std::wstring &message)
: level(level), logger_name(logger.GetName()), message(message), sequence_number(LogRecord::record_count++), source_class_name(), source_method_name(), time(std::chrono::system_clock::now()) {
}
LogRecord::LogRecord(const Logger &logger, LogLevel level, std::wstring &&message)
: level(level), logger_name(logger.GetName()), message(message), sequence_number(LogRecord::record_count++), source_class_name(), source_method_name(), time(std::chrono::system_clock::now()) {
}
LogRecord::LogRecord(const Logger &logger, LogLevel level, const std::wstring &message, const std::wstring &source_class_name, const std::wstring &source_method_name)
: level(level), logger_name(logger.GetName()), message(message), sequence_number(LogRecord::record_count++), source_class_name(source_class_name), source_method_name(source_method_name), time(std::chrono::system_clock::now()) {
}
LogRecord::LogRecord(const Logger &logger, LogLevel level, std::wstring &&message, const std::wstring &source_class_name, const std::wstring &source_method_name)
: level(level), logger_name(logger.GetName()), message(message), sequence_number(LogRecord::record_count++), source_class_name(source_class_name), source_method_name(source_method_name), time(std::chrono::system_clock::now()) {
}
LogRecord::LogRecord(const Logger &logger, LogLevel level, const std::wstring &message, std::wstring &&source_class_name, const std::wstring &source_method_name)
: level(level), logger_name(logger.GetName()), message(message), sequence_number(LogRecord::record_count++), source_class_name(source_class_name), source_method_name(source_method_name), time(std::chrono::system_clock::now()) {
}
LogRecord::LogRecord(const Logger &logger, LogLevel level, std::wstring &&message, std::wstring &&source_class_name, const std::wstring &source_method_name)
: level(level), logger_name(logger.GetName()), message(message), sequence_number(LogRecord::record_count++), source_class_name(source_class_name), source_method_name(source_method_name), time(std::chrono::system_clock::now()) {
}
等
有没有更好的方法来解决这个问题而不是简单地为每个可能的组合声明一个构造函数,比如这个?
class LogRecord {
public:
LogRecord(const Logger &logger, LogLevel level, const std::wstring &message);
LogRecord(const Logger &logger, LogLevel level, std::wstring &&message);
LogRecord(const Logger &logger, LogLevel level, const std::wstring &message, const std::wstring &source_class_name, const std::wstring &source_method_name);
LogRecord(const Logger &logger, LogLevel level, std::wstring &&message, const std::wstring &source_class_name, const std::wstring &source_method_name);
LogRecord(const Logger &logger, LogLevel level, const std::wstring &message, std::wstring &&source_class_name, const std::wstring &source_method_name);
LogRecord(const Logger &logger, LogLevel level, std::wstring &&message, std::wstring &&source_class_name, const std::wstring &source_method_name);
LogRecord(const Logger &logger, LogLevel level, const std::wstring &message, const std::wstring &source_class_name, std::wstring &&source_method_name);
LogRecord(const Logger &logger, LogLevel level, std::wstring &&message, const std::wstring &source_class_name, std::wstring &&source_method_name);
LogRecord(const Logger &logger, LogLevel level, const std::wstring &message, std::wstring &&source_class_name, std::wstring &&source_method_name);
LogRecord(const Logger &logger, LogLevel level, std::wstring &&message, std::wstring &&source_class_name, std::wstring &&source_method_name);
...
private:
std::wstring message, source_class_name, source_method_name;
...
};
这是一个简化的表格,使其更容易阅读。
Object
是包含成员的类,Member
是成员的类型名称。 Member
类型同时定义了复制构造函数和移动构造函数。
基本上,我的问题是如何通过减少代码重复来完成以下操作:
class Object {
public:
Object(const Member &x, const Member &y, const Member &z) : x(x), y(y), z(z) {}
Object(Member &&x, const Member &y, const Member &z) : x(x), y(y), z(z) {}
Object(const Member &x, Member &&y, const Member &z) : x(x), y(y), z(z) {}
Object(Member &&x, Member &&y, const Member &z) : x(x), y(y), z(z) {}
Object(const Member &x, const Member &y, Member &&z) : x(x), y(y), z(z) {}
Object(Member &&x, const Member &y, Member &&z) : x(x), y(y), z(z) {}
Object(const Member &x, Member &&y, Member &&z) : x(x), y(y), z(z) {}
Object(Member &&x, Member &&y, Member &&z) : x(x), y(y), z(z) {}
private:
Member x, y, z;
}
答案 0 :(得分:2)
我不打扰所有那些重载。始终按值std::wstring
参数和std::move
在mem-initializer中使用它们。那么你只需要3个构造函数定义。需要注意的是,在你通过左值的情况下,你会产生一个额外的移动结构,但是你很可能会接受它。
LogRecord(const Logger &logger, LogLevel level, std::wstring message)
: level(level), logger_name(logger.GetName()), message(std::move(message)), ...
{}
请注意,由于小字符串优化,移动构造实际上可能是 O(n) n 的小值。
另一个选项是评论中提到的完美转发。你可以做点什么
template<typename Message>
LogRecord(const Logger &logger, LogLevel level, Message&& message)
: level(level), logger_name(logger.GetName()), message(std::forward<Message>(message)), ...
{}
也许可以添加static_assert
来打印Message
所有或可转换为std::wstring
的更好的错误消息。