Type specific code compilation within function
-
Hi,
If constructing function that has variadic parameter list and some input parameters are not compatable with function calls within the variadic function.What methods can be used within same function to instruct compiler to compile specific code for each input variable type?
I understand template functions could be used but it means lot more code.
Perhaps lambdas support variable specific execution or something similar.
Currently variables are filtered using
typeid(xx).name()
but not all code is compatable with all variable types. -
Hi,
If constructing function that has variadic parameter list and some input parameters are not compatable with function calls within the variadic function.What methods can be used within same function to instruct compiler to compile specific code for each input variable type?
I understand template functions could be used but it means lot more code.
Perhaps lambdas support variable specific execution or something similar.
Currently variables are filtered using
typeid(xx).name()
but not all code is compatable with all variable types.@Q139 said in Type specific code compilation within function:
Hi,
If constructing function that has variadic parameter list and some input parameters are not compatable with function calls within the variadic function.What sort of variadic are we talking about? Example code please!
What methods can be used within same function to instruct compiler to compile specific code for each input variable type?
SFINAE if you are working with templates, otherwise (i.e. C variadics), none, they're not and will never be type-safe.
I understand template functions could be used but it means lot more code.
Could be used. A lot more code is a matter of debate, depending on the code, implementation, etc.
Perhaps lambdas support variable specific execution or something similar.
I have no idea what you mean. C++ is statically typed, so every type, no matter how complicated it may be shall be known at compile time.
Currently variables are filtered using
typeid(xx).name()
but not all code is compatable with all variable types.This is a runtime-evaluated operator. It basically resolves the platform specific decorated type name. However I don't understand what is the relation to the top part of this post. Elaborate please.
-
@Q139 said:
What methods can be used within same function to instruct compiler to compile specific code for each input variable type?
Lets say you want to do something for ints and something else for all other types:
void doSomethingWithInt(int) { qDebug() << "I'm an int"; } void doSomethingElse(auto) { qDebug() << "I'm not an int"; }
You can choose the right function at compile time with
if constexpr
and type traits:template <typename T> void doStuffToOneItem(T t) { if constexpr (std::is_same_v<T,int>) doSomethingWithInt(t); else doSomethingElse(t); }
and then you can use it in a variadic function with the usual ugly parameter pack shenanigans:
template <typename ...Args> void foo(Args&&... args) { std::initializer_list<int>{(doStuffToOneItem(std::forward<Args>(args)), 0)...}; }
and call it:
foo("hello", 42, 17.3f);