mardi 24 décembre 2019

How to call the right constructor of a template type?

In the following code, how can I make the commented line working in the same way as the line just above of it?

I would like to make it a generic code, that call suitable constructor of a template Type.

#include <string>
#include <iostream>

template <typename Type>
struct Class
{
    Type data;
    Class(Type data) : data(data) { }
};

int main()
{
    Class<std::string> a = std::string("abc");
    // Class<std::string> b = "abc";
    std::cout << a.data << std::endl;
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire