Check member functions signature in compile time [duplicate] - c++

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is it possible to write c++ template/macros to check whether two functions have the same signatures
Is it possible to write c++ template/macros to check whether two member functions have the same signatures (return type and arguments list) in compile time ?
I want something like this:
CHECK_SIGNATURES(Foo, foo, Bar, bar);
Compilation fails if Foo::foo and Bar::bar functions have deifferent return types or parameters list.

Try the following:
template <class T>
bool same(T, T) { return true; }
template <class T, class U>
bool same(T, U) { return false; }
Or maybe you can use std::is_same like in the answer to the duplicate question.

Related

Use static_assert to ensure CRTP method is implemented [duplicate]

This question already has answers here:
Is emulating pure virtual function in static polymorphism using CRTP possible?
(3 answers)
Templated check for the existence of a class member function?
(33 answers)
Closed 1 year ago.
I am using CTRP to define an interface in C++ as follows:
template <typename T>
class Interface {
public:
inline void foo() { static_cast<T*>(this)->foo(); }
};
class Implementation : public Interface<Implementation> {
public:
void foo();
};
Thus, if the Interface is used as such:
template <typename T>
void interfaceUser(Interface<T>& i) {
i.foo();
}
This will end up calling the foo() method of the implementation.
However, the flaw in this method (which I would like to keep because the method names of the interface and implementation are the same), is that, if the implementation does not implement the method, the code still compiles but causes an infinite loop.
My question is, is there some way to use static_assert to compare the pointers of Interface::foo and static_cast<T*>(this)->foo to ensure that they are NOT the same and thus, that the function has been implemented?
Also, please let me know if I am going about this in completely the wrong way.
Thanks

How to make a callback in C++: use a class member function as a parameter [duplicate]

This question already has answers here:
Callback functions in C++
(11 answers)
Closed 2 years ago.
I started using C++ recently and at one point I needed to set up a callback, i.e. call a function of another class and pass to it as a parameter the name of one of my own functions so that it calls it back. At first I tried this:
void myOtherFunction(void (*oneOfMyFunctions)(void)) {
oneOfMyFunctions();
}
Unfortunately this code doesn't support class member functions because it is (correct me if I am wrong) ...C code.
This can work.
void myOtherFunction(void (*oneOfMyFunctions)(void)) {
oneOfMyFunctions();
}
However, your problem may be due to trying to pass member functions into this function. If member_function is a member function of class A, the expression &member_function inside class A has a type of void (A::*)(void), not void (*)(void) like you want (that is, it wants an A pointer in addition to its normal parameters). You can use std::bind():
std::bind(&member_function, this)
to create a function object which can be called with an empty parameter list. However, then you would need to change your member function signature to something like this:
template <typename FuncType>
void myOtherFunction(FuncType oneOfMyFunctions) {
oneOfMyFunctions();
}
or, like Th0rgal may have said,
void myOtherFunction(std::function<void()> oneOfMyFunctions) {
oneOfMyFunctions();
}
Here is a working way to do that:
void myOtherFunction(std::function<void()> oneOfMyFunctions) {
oneOfMyFunctions();
}
And inside my class:
myOtherFunction([&] {
oneOfMyFunctions();
});
Some explanations:
In std::function<void()>, void is what is returned by the function and () contains the types of its parameters (mine is empty because it doesn't have any).
In the 2nd code I am using a lambda to keep the context, as a bind would do (but lambdas replace them).

How can I check whether a class inherits another class? [duplicate]

This question already has answers here:
Check if class is derived from a specific class (compile, runtime both answers available)
(6 answers)
Closed 3 years ago.
Suppose I'm writing the following templated function:
class A { /* ... */ };
// ... etc...
template<typename C>
void foo() {
bool C_inherits_A = /* magic */;
if (C_inherits_A) { do_something(); }
else { do_something_else(); }
}
We remember dynamic_cast from the olden days, but that isn't relevant here since there's no pointer, and I'm checking "downward", not "upward". Is there something simple with which to replace /* magic */ in the snippet above?
PS - There should definitely already be a dupe of this question, but I just could not find one so I wrote it up.
Beginning in C++11, the standard C++ library caters to this exact need - using the std::is_base_of type trait. To read a bit more about type traits see their SO tag page.
Anyway, you would replace /* magic */ with:
std::is_base_of<A, C>::value
which is a Boolean expression that's true if A is a base class of C, i.e. if C inherits A.
Remember that type traits are evaluated at compile-time, so you can use if (std::is_base_of<A,C>::value) in constexpr functions, or in template parameters and so on.

callback function syntax [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What does void(U::*)(void) mean?
Considering the following:
template <class T>
class myButtoncb {
private:
T *ptr;
void (T::*cback) (void)
}
What I understand is:
void (*cback) (void)
Which is nothing but a function pointer that to a function that returns void, and takes no argument.
What I dont understand is, what is the importance of T::? Isn't it enough to declare
only like void (*cback) (void) ?
This says that it's a member function that has a this pointer. Otherwise, it would be a free function, wouldn't have any idea what object it was operating on, and wouldn't be able to access any non-static member functions or member variables.
From C++ FAQ
Is the type of "pointer-to-member-function" different from "pointer-to-function"?
Yep.
Link which I've provided to you has a lot of information about this topic.
The function, you pass there, must be declared inside the class T - the template parameter of myButtoncb. So you can use a function like the following:
class A
{
public:
void foo(void);
};
myButton<A> b;
b.cback = &A::foo;

C++: template params for a class but not a function [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Template type deduction in C++ for Class vs Function?
When calling a template function, you don't need to specify the template parameters if they are non-ambiguous from your parameters. E.g. for:
template<typename T> T foo(T a) { /*...*/ }
You can just call foo(1) and it will work, it does not need to be foo<int>(1).
This is not true for classes/structs, even if it would be clear from the constructor parameters. For example:
template<typename T> struct Foo { Foo(T a) { /*...*/ } };
Now I cannot do just a do_something_with(Foo(1)), it must be do_something_with(Foo<int>(1)).
Often, to work around this issue, there are just some simple wrapper functions which basically just wrap the constructor. That is even in the STL: std::make_pair is such an example.
Now the question: Why is that? Is there any rational reason behind it?
As far as I know, function templates and class templates are different for the lulz and there's no real reason that they should be different from each other. Of course, class templates have partial specializations (T*) as an advantage.