Speaking of pair and tuple...
C++14 | C++17 |
---|---|
pair<int, string> is1 = pair<int, string>(17, "hello");
auto is2 = std::pair<int, string>(17, "hello");
auto is3 = std::make_pair(17, string("hello"));
auto is4 = std::make_pair(17, "hello"s); |
pair<int, string> is1 = pair(17, "hello");
auto is2 = pair(17, "hello"); // !! pair<int, char const *>
auto is3 = pair(17, string("hello"));
auto is4 = pair(17, "hello"s); |
The magic behind the above is called "deduction guides". In particular, implicit deduction guides, and explicit deduction guides.
template<typename T>
struct Thingy
{
T t;
};
// !! LOOK HERE !!
Thingy(const char *) -> Thingy<std::string>;
Thingy thing{"A String"}; // thing.t is a `std::string`.
(example from "Nicol Bolas")
For any template<typename T, typename U, etc> struct
... (or class!)
if there is a constructor that takes T
and U
such that it can figure out all the types,
then that constructor forms an "implicit" deduction guide. ie just like the explicit one above, but the compiler does it for you.
More importantly, the above should say for all templatized types... ie whether you want it or not.