Category : function-templates

Hello I have this example: #include <iostream> template <typename T> int compare(T const&, T const&); template <typename T> int compare(T*, T*); template <> int compare(char const * const&, char const* const&); template <unsigned N, unsigned M> int compare(char const (&)[N], char const(&)[M]); template <unsigned N> int compare(char const (&)[N], char const(&)[N]); template <typename T> int ..

Read more

I’m trying to make a function template in a header file that accepts a generic function pointer and packed arguments. The function template would invoke the received function pointer using the packed arguments. My goal is to calculate and return the execution time of the function pointer. #ifndef LOGTIME_HPP #define LOGTIME_HPP #include <chrono> #include <ratio> ..

Read more

I am thinking to write a function that works both for sequence and associative container. That’s something like template<class C, class V = typename C::key_type> bool has_val(const C& c, const V& v)` Inside the function, I would like to Check if class C has member function const_iterator find(key_type) const for container classes like set/map. If ..

Read more

I am writing a template function like that: template<typename T> std::string EncodeData(int DataType, T Data, std::string ReadCommandID, std::string& ThisID); The type T that I want to use is some different structs, for example: struct A { int A_data; bool A_data_2; // etc……. }; struct B { double B_data; char B_data_2; // etc……. }; I want ..

Read more

Suppose I have a template function and it’s specialization. template<class T> void myfunction(T whatever) { } template<> void myfunction(int whatever) { //whatever } As far as I know, if I never call myfunction is the my programm, the template will never be instantiated and will go into the binary code only regarded as a template(correct ..

Read more

question Is there a way to write a templated function accepting either a template value parameter (for statically known values), or either a classic function argument (for dynamic values)? I would imagine something callable like this, allowing the "static" version to be optimised by the compiler. static_dynamic_fun<T>(a, b, default_fun<T>); // Use function argument static_dynamic_fun<T, default_fun<T>>(a, ..

Read more