Is it possible to emulate template?

After your update: no. There is no such functionality in C++. The closest is macros:

#define AUTO_ARG(x) decltype(x), x

f.bar<AUTO_ARG(5)>();
f.bar<AUTO_ARG(&Baz::bang)>();

Sounds like you want a generator:

template <typename T>
struct foo
{
    foo(const T&) {} // do whatever
};

template <typename T>
foo<T> make_foo(const T& x)
{
    return foo<T>(x);
}

Now instead of spelling out:

foo<int>(5);

You can do:

make_foo(5);

To deduce the argument.

Leave a Comment