Why can I typedef a template and construct it without declaring a constructor, but can't extend a template as a class and construct it without declaring a constructor?
In this example, the typedef works but the class does not. Surely somewhere C++ is generating a constructor for the typedef - why can't it do the same for the class?
#include <iostream>
#include <string>
template <class T>
class Event {
public:
Event(T data){this->data = data; std::cout<<data;}
virtual ~Event(){}
T data;
};
typedef Event<std::string> StringEvent;
class MyEvent : public Event<std::string> {};
int main()
{
StringEvent event("hi"); // ok
MyEvent event2("hi"); // no
return 0;
}
Output
main.cpp: In function ‘int main()’:
main.cpp:21:24: error: no matching function for call to ‘MyEvent::MyEvent(const char [3])’
MyEvent event2("hi");
^
main.cpp:14:7: note: candidate: MyEvent::MyEvent(const MyEvent&)
class MyEvent : public Event<std::string> {};
^~~~~~~
main.cpp:14:7: note: no known conversion for argument 1 from ‘const char [3]’ to ‘const MyEvent&’
main.cpp:14:7: note: candidate: MyEvent::MyEvent(MyEvent&&)
main.cpp:14:7: note: no known conversion for argument 1 from ‘const char [3]’ to ‘MyEvent&&’