#include <iostream>
struct MyStruct
{
template<typename T>
MyStruct(T t)
{
std::cout << "T : ";
}
};
template<>
MyStruct::MyStruct(int i)
{
std::cout << "int : ";
}
template<>
MyStruct::MyStruct(double d)
{
std::cout << "double : ";
}
int main()
{
MyStruct myStructInt(1);
MyStruct myStructDouble(2.2);
MyStruct myStructString("Hello");
std::cout << std::endl;
return 0;
}
// Output: int : double : T :
Reference: https://en.wikipedia.org/wiki/C%2B%2B17Tuesday, October 17, 2017
C++17: Template deduction of constructors
C++17 allows template deduction of constructors. Here is an example:
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment