c++ - Is it possible to emulate template<auto X>? -
is somehow possible? want enable compile-time passing of arguments. suppose it's user convenience, 1 type out real type template<class t, t x>, types, i.e. pointer-to-member-functions, it's pretty tedious, decltype shortcut. consider following code:
struct foo{ template<class t, t x> void bar(){ // x, compile-time passed } }; struct baz{ void bang(){ } }; int main(){ foo f; f.bar<int,5>(); f.bar<decltype(&baz::bang),&baz::bang>(); } would somehow possible convert following?
struct foo{ template<auto x> void bar(){ // x, compile-time passed } }; struct baz{ void bang(){ } }; int main(){ foo f; f.bar<5>(); f.bar<&baz::bang>(); }
after update: no. there no such functionality in c++. closest macros:
#define auto_arg(x) decltype(x), x f.bar<auto_arg(5)>(); f.bar<auto_arg(&baz::bang)>(); sounds want generator:
template <typename t> struct foo { foo(const t&) {} // 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 argument.
Comments
Post a Comment