block a function when argument type does not belong to a set of types

Debashish
:

In a specific requirement I need to allow instantiation of a template function func only for a specific set of allowed types.

Therefore I tried to use already available std::type_traits and then used std::enable_if to block instantiation. Here is my implementation.

// C++17
#include <vector>
#include <cstdint>
#include <iostream>
#include <type_traits>

using my_int_t      = std::int16_t;         // T1
using my_string_t   = std::string;          // T2
using my_vec_t      = std::vector<int>;     // T3
// and so on .. a long list of types 


/* Check input Type T : if it belongs to {T1,T2,T3} 
If it belongs then only allow this version of func to instantiate
*/
template<typename T, typename T1,typename T2,typename T3>
using isCompatible = std::bool_constant<std::is_same_v<T,T1>
                            || std::is_same_v<T,T2>
                            || std::is_same_v<T,T3> >;

template<typename T>
using CompatibleTypeCheck = std::enable_if_t< isCompatible<std::remove_reference_t<T>,
                                                    my_int_t,
                                                    my_string_t,
                                                    my_vec_t>::value >;



template<typename T,
        typename = CompatibleTypeCheck<T>>
void func(T&& val) {
    std::cout <<"Hello from Generic Func" << std::endl;
}


int main() {

    // int z = 10;   
    // func(z); // Error ! as expected

    my_int_t w = 100;
    func(w); // OK
}

But the problem is there are too many allowed types. Is there is better way to clean both of the alias templates ? i.e. isCompatible and CompatibleTypeCheck.

If there is any better "idiomatic" way to achieve the same result, please suggest it.

cigien
:

You can write isCompatible like this:

template<typename T, typename ...Ts> 
using isCompatible = std::bool_constant<(std::is_same_v<T,Ts> || ...)>;

There's not much you can do for CompatibleTypeCheck since you need to specify all the allowed types somewhere.

Here's a demo.


Note that enable_if is usually used when you want to enable or disable particular overloads. In your case, the easier way would be to just static_assert inside the function definition:

template<typename T>
void func(T&& val) {
    static_assert(isCompatible<std::remove_reference_t<T>,
                                                    my_int_t,
                                                    my_string_t,
                                                    my_vec_t>::value);
    std::cout <<"Hello from Generic Func" << std::endl;
}

which avoids the need for CompatibleTypeCheck entirely.

Here's a demo.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why does this Scala function compile when the argument does not conform to the type constraint?

From Dev

How to conditionally set a function argument type in typescript?

From Dev

How to make a specific type belong to a family of types?

From Dev

Is it possible for a function to only accept a limited set of types for a given argument?

From Dev

Is it possible for a function to only accept a limited set of types for a given argument?

From Dev

When shelving changes to a branch in TFS, does the shelf-set belong to the branch?

From Dev

Which library does boost::type belong to?

From Dev

Why an inherited field does not belong to the final type

From Dev

Where does the Type Annotation belong in a bounded wildcard?

From Dev

Does the system() function belong to C or C++?

From Dev

Does the system() function belong to C or C++?

From Dev

Unresolved overloaded function type when attempting to pass a function as an argument

From Dev

Unresolved overloaded function type when attempting to pass a function as an argument

From Dev

set function output type based on whether optional keyword argument present

From Dev

set function output type based on whether optional keyword argument present

From Dev

Why does Haskell want this function's argument to have the type classes (RealFrac x, Integral x) when it only needs to be Integral x?

From Dev

What does () mean as an argument in a function where a parameter of type T is expected?

From Dev

Dynamically set Type when calling constructor with predefined types

From Dev

"ByRef argument type mismatch" when passing For Each iteration Object to Function

From Dev

Understanding decorators: return type is a function when argument not specified

From Dev

"Error: The function applied to this argument has type ..." when using named parameters

From Dev

vba Byref argument type mismatch when calling the function

From Dev

Error when using rref: "Undefined function for input argument of type 'cell'."

From Dev

Throw an error/warning when supplying wrong argument type to C function

From Dev

Throw an error/warning when supplying wrong argument type to C function

From Dev

"Error: The function applied to this argument has type ..." when using named parameters

From Dev

Accept all types as argument in function

From Dev

React TS function argument types

From Dev

Differents argument types on the same function

Related Related

  1. 1

    Why does this Scala function compile when the argument does not conform to the type constraint?

  2. 2

    How to conditionally set a function argument type in typescript?

  3. 3

    How to make a specific type belong to a family of types?

  4. 4

    Is it possible for a function to only accept a limited set of types for a given argument?

  5. 5

    Is it possible for a function to only accept a limited set of types for a given argument?

  6. 6

    When shelving changes to a branch in TFS, does the shelf-set belong to the branch?

  7. 7

    Which library does boost::type belong to?

  8. 8

    Why an inherited field does not belong to the final type

  9. 9

    Where does the Type Annotation belong in a bounded wildcard?

  10. 10

    Does the system() function belong to C or C++?

  11. 11

    Does the system() function belong to C or C++?

  12. 12

    Unresolved overloaded function type when attempting to pass a function as an argument

  13. 13

    Unresolved overloaded function type when attempting to pass a function as an argument

  14. 14

    set function output type based on whether optional keyword argument present

  15. 15

    set function output type based on whether optional keyword argument present

  16. 16

    Why does Haskell want this function's argument to have the type classes (RealFrac x, Integral x) when it only needs to be Integral x?

  17. 17

    What does () mean as an argument in a function where a parameter of type T is expected?

  18. 18

    Dynamically set Type when calling constructor with predefined types

  19. 19

    "ByRef argument type mismatch" when passing For Each iteration Object to Function

  20. 20

    Understanding decorators: return type is a function when argument not specified

  21. 21

    "Error: The function applied to this argument has type ..." when using named parameters

  22. 22

    vba Byref argument type mismatch when calling the function

  23. 23

    Error when using rref: "Undefined function for input argument of type 'cell'."

  24. 24

    Throw an error/warning when supplying wrong argument type to C function

  25. 25

    Throw an error/warning when supplying wrong argument type to C function

  26. 26

    "Error: The function applied to this argument has type ..." when using named parameters

  27. 27

    Accept all types as argument in function

  28. 28

    React TS function argument types

  29. 29

    Differents argument types on the same function

HotTag

Archive