Template function dependent on non-type parameter

mbed_dev

Is it somehow possible to define an overloaded template function based on a non-type parameter?

following situation:

template<uint8_t> void SetupMem();

template<> void SetupMem<4>()
{ /* some code */ }


template<> void SetupMem<8>()
{ /* some code */ }

void TemplateCaller()
{
   // use the plattform-specific template function at compile-time
   SetupMem<sizeof(size_t)>();
}

now is it somehow possible to change the return value of SetupMem based on the non-type parameter? e.g.:

template<> uint32_t SetupMem<4>(){}
template<> uint64_t SetupMem<8>(){}

So that TemplateCaller() does not explicitly calls SetupMem with the desired template parameter (so avoiding something like: SetupMem<uint64, sizeof(size_t)>();)? Possible solutions upto C++11 are welcome :)

Vittorio Romeo

Just use simple function overloading and std::integral_constant:

std::uint32_t SetupMem(std::integral_constant<int, 4>); // (0)
std::uint64_t SetupMem(std::integral_constant<int, 8>); // (1)

void TemplateCaller()
{
   auto a = SetupMem(std::integral_constant<int, 4>{}); // calls (0)
   auto b = SetupMem(std::integral_constant<int, 8>{}); // calls (1)
}

You can introduce a template type alias for readability:

template <int X>
using ic = std::integral_constant<int, X>;

std::uint32_t SetupMem(ic<4>);
std::uint64_t SetupMem(ic<8>);

void TemplateCaller()
{
   auto a = SetupMem(ic<4>{});
   auto b = SetupMem(ic<8>{});
}

live example on wandbox.org


If your compiler doesn't support integral_constant, all you need to do is define it yourself:

template <int>
struct ic { };

std::uint32_t SetupMem(ic<4>);
std::uint64_t SetupMem(ic<8>);

void TemplateCaller()
{
   auto a = SetupMem(ic<4>{});
   auto b = SetupMem(ic<8>{});
}

live example on wandbox.org

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Non-type template parameter dependent on a default template type parameter

Partial specialization of single type template parameter class template using non-type template parameter through a dependent type

Function template taking a template non-type template parameter

why __dependent_type from libcxx uses a template non-type parameter bool _Dummy?

static_assert in function template with non-type template parameter

Non type template parameter

static_assert dependent on non-type template parameter (different behavior on gcc and clang)

deduce type of variable dependent on template function type

Function template overload resolution, dependent and non-dependent parameters

Passing a function pointer as non-type template parameter

Type-dependent constant in template function

Type trait dependent specialization of template function

Can placeholder type in non-type template parameter involve overload resolution of the function passed as a template argument?

refer to a non-dependent name without specifying template parameter

template non-type template parameter

Alias Template with non-type template parameter

Specialize template template parameter with a non-type template parameter

Why is initialization of a constant dependent type in a template parameter list disallowed by the standard?

Why can't I specialize with a nullptr template parameter on a dependent type?

Calling template function with non-type template parameter using class attribute

C++ - specialize function template on a templated class with a non type template parameter

Dependent non-type parameter packs: what does the standard say?

C++ Make function call dependent on template parameter

How to have a member function implementation dependent on class' template parameter?

Perfect forwarding of lambda arguments to member function, where member function is a non-type template parameter

Type dependent template name

Using non-type template template parameter in template specialisation

non type template parameter pack expansion

pointer non-type template parameter

TOP Ranking

HotTag

Archive