template <typename T>
struct Message {
T data;
explicit Message(T&& data) : data(std::move(data)) {
std::cout << "Move data" << std::endl;
}
explicit Message(const T& data) : data(data) {
std::cout << "Copy data" << std::endl;
}
};
template <typename T>
inline Message<T>*
makeMessage(T&& data) {
return new Message<T>{std::forward<T>(data)};
}
int main() {
const int a = 1024;
auto *copy_msg = makeMessage(a);
}
There is a template class Message
that has two constructors: Message(T&& data)
and Message(const T& data)
, And I got the following compile-time errors when I called makeMessage(a)
.
error: multiple overloads of 'Message' instantiate to the same signature 'void (const int &&)'
explicit Message(const T& data) : data(data) {
previous declaration is here
explicit Message(T&& data) : data(std::move(data)) {
However, It works when I called make_message(1024)
and make_message(std::move(a))
.
So why constructor Message(const T& data)
is duplicate with Message(T&& data)
when T = int&?
Aucun commentaire:
Enregistrer un commentaire