Using free function as pseudo-constructors to exploit template parameter deduction

Posted by Poita_ on Stack Overflow See other posts from Stack Overflow or by Poita_
Published on 2010-03-17T22:59:46Z Indexed on 2010/03/17 23:01 UTC
Read the original article Hit count: 294

Is it a common pattern/idiom to use free functions as pseudo-constructors to avoid having to explicitly specify template parameters?

For example, everyone knows about std::make_pair, which uses its parameters to deduce the pair types:

template <class A, class B>
std::pair<A, B> make_pair(A a, B b)
{
  return std::pair<A, B>(a, b);
}

// This allows you to call make_pair(1, 2),
// instead of having to type pair<int, int>(1, 2)
// as you can't get type deduction from the constructor.

I find myself using this quite often, so I was just wondering if many other people use it, and if there is a name for this pattern?

© Stack Overflow or respective owner

Related posts about c++

Related posts about templates