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.
Related
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
This question already has answers here:
C++ template constructor
(10 answers)
Closed 9 years ago.
A templated member function, with template arguments not used in the parameter list can be called in the following form:
struct C { template <class> func (); };
C c;
C.func <int>();
But how do I call a template constructor, which does not use a template parameter in its argument list?
struct D { template <class> D (); };
Certainly
D<int> d;
Cannot be the syntax, as this is the construction of a variable of type D <int>,, which is an instantiation of class template D<class>.
This is not just an academic question, I have use for templated constructors (not using the template in the constructor argument list), basically policybased factories and currently use a dummy parameter mpl::identity <mytype>() as a workaround.
This is not my own knowledge, but instead taken from a few other sources, mainly the already posted C++ template constructor.
I assume, that it is not possible to instanciate template-constructors without parameters, because this could create multiple default constructors. Templates are expanded at compile-time and thus create some kind of overload on the function they create. The default-constructor cannot be overloaded, so this has to fail when you use more than one template-instance.
Apart from the dummy-variable I can only think of using a templated factory method or class (if that is possible in your case)
ex: (Using an int-template instead of class-template, since I can't think of another example at the moment)
class C
{
int i;
C() { }
public:
template<int I>
static C newInstance()
{
C c;
c.i = I;
return 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.
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;
This question already has answers here:
Restrict C++ Template Parameter to Subclass
(8 answers)
Closed 7 years ago.
Is there any way to make a template only work with child classes of a base class? Something like this:
template<BaseClass T>
class FooBar
{
// ...
};
Either use a static assert from your favourite C++ library (such as this boost example), or put a call in the constructor (or other code which will always be generated when the code is used) to a do-nothing function taking a BaseClass type, for example:
template<class T>
class FooBar
{
public:
FooBar () {
Check(static_cast<T*>(0));
}
private:
void Check ( BaseClasse* ) {}
};
(not tested)
Not directly, but you can test it in the constructor using Boost:
#include <boost/type_traits/is_base_of.hpp>
#include <boost/static_assert.hpp>
template<class T>
class FooBar{
FooBar(){
BOOST_STATIC_ASSERT(boost::is_base_of<BaseClass,T>::value);
}
};
Or if you don't want a static assert, something like this is also nice sometimes:
typedef char ERROR_T_must_be_derived_from_BaseClass[boost::is_base_of<BaseClass,T>::value ? 1 : -1];
Since whoever compiles your code will be brought to this line and has a "readable" error message.
There are ways to make it work with a static assert. See Boost.StaticAssert