Assessment of Template Parameter [duplicate] - c++

This question already has answers here:
Identifying primitive types in templates
(8 answers)
Closed 6 years ago.
Is there a ready function which can take a template parameter as an argument and determine that it is a user defined types(class or struct) or built-in data types(int, float, char...) ?

You're looking for the std::is_arithmetic template, which determines if the template parameter is an integer or a floating point type.
By process of elimination, the only remaining options are: pointer or a reference, a class, and void. Maybe an enum of some kind, too. It's unclear from your question how you want to classify those, but, if necessary, adding some additional checks on top of std::is_arithmetic should make it possible to disambiguate the given type further.

Related

trait is_same for arbitrary many types [duplicate]

This question already has answers here:
How to make a variadic is_same?
(6 answers)
How to create compile time check for multiple C++ types?
(3 answers)
Closed 3 months ago.
The type_traits library offers the possibility to check if two type are the same with is_same.
How can one generalize to an arbitrary long list of types? Something declared as
template <class... U>
struct is_same_many;
with a field value such that value = true if all Us are the same, and false otherwise.

C++ How To Make Template Accept Certain Types [duplicate]

This question already has answers here:
How to check at compile-time inheritance using std::enable_if_t & std::is_base_of for SFINAE?
(1 answer)
C++ template function for derived class with std::is_base_of
(4 answers)
Closed 11 months ago.
I am making a entity component system, Where I can add a Object/Type to my entity:
template<typename ComponentType>
void addComponent(){}
But with this approach, I can make any object and send it as a parameter and I dont want that. I want only certain types to be allowed, but more specifically, I want all the objects/types that extend Component to be allowed.
Note: that Component will be a base class for all other types that extend this.
Long story short how do I make this so I can only pass types to the template that are a child of this base class???
Is this possible?

How do I typedef a function type that returns a function of the same type in C++? [duplicate]

This question already has answers here:
Function Returning Itself
(10 answers)
Closed 3 years ago.
I would like to know how to declare a function type that returns a function of the same type using typedef in C++.
While watching a Golang talk given by Rob Pike on lexical scanning, I came across the following code snippet,
type stateFn func(*Scanner) stateFn
This is the exact behavior that I would like to replicate in C++.
I have tried using the following type definition,
typedef state_fn state_fn(Scanner &);
But this gives me the error function returning function is not allowed.
Is it possible at all to do something like this? If not, how do I do something similar?
You cannot return functions in C++. See [dcl.fct]p10:
Functions shall not have a return type of type array or function, although they may have a return type of type pointer or reference to such things.
However, you can return some form of callable (that may be wrapping a function of the same type).

C++ Functors explanation [duplicate]

This question already has answers here:
What are C++ functors and their uses?
(14 answers)
Closed 7 years ago.
Could you please explain following statements with example
Statement1
Ordinary functions have different types only when their signatures differ. However, function objects can have different types
when their signatures are the same. In fact, each functional behavior
defined by a function object has its own type. This is a significant
improvement for generic programming using templates because you can
pass functional behavior as a template parameter
Ordinary functions have different types only when their signatures differ.
Meaning that these functions both have the same type int(int):
int f1(int);
int f2(int);
while this has a different type void(int)
void f3(int);
However, function objects can have different types when their signatures are the same.
Meaning that these two classes are different types (as different classes always are):
class c1 {int operator()(int);};
class c2 {int operator()(int);};
In fact, each functional behavior defined by a function object has its own type.
I don't know exactly what the author means by "functional behaviour", but I think this is just restating that two class types are separate types.
This is a significant improvement for generic programming using templates because you can pass functional behavior as a template parameter
Meaning that you can specify a function class as a template type parameter:
temp<c1> thing_using_c1;
temp<c2> thing_using_c2;
But you can't specify a plain function that way, you'd need to supply the function some other way:
temp<int(int)> thing_using_f1(f1);
temp<int(int)> thing_using_f2(f2);
different classes can have different functions with the same name and its parameters. but in general non class functions thats not possible

explicit keyword [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
What does the explicit keyword in C++ mean?
what does the keyword explicit mean?
C++ constructors that have just one parameter automatically perform implicit type conversion. For example, if you pass an int when the constructor expects a string pointer parameter, the compiler will add the code it must have to convert the int to a string pointer. However, you might not always want this automatic behavior.
You can add explicit to the constructor declaration to prevent implicit conversions. This forces the code to either use a parameter of the correct type, or cast the parameter to the correct type. That is, if the cast is not visibly expressed in code, an error will result.
explicit (C++)