Say I have a templated class:
template <typename T>
class foo {
void do_someting(T obj) {
// do something generic...
}
};
and I want to specialize do_something, but within it I want to call the "normal" do_something function:
template<>
void foo<MyObj>::do_something(MyObj obj) {
// do something specific...
// and ALSO do something generic!
}
is there a way to refer to the normal version of do_something within my specialized function? Or do I just have to copy the code?
(I know that I could refactor foo in such a way that I wouldn't have this exact problem, but as it happens I can't really modify the "real" foo, as it's heavily-shared code.)
No. Your specialization is the only definition that will exist for the MyObj type argument. But, consider modifying the foo template in this manner, which will be transparent to the current users of the template:
template<typename T>
class foo {
void prelude(T &obj){ // choose a better name
/* do nothing */
}
void do_something(T obj){
prelude(obj);
// do something generic...
}
};
Then define a specialization for the prelude:
template<>
void foo<MyObj>::prelude(MyObj &obj){
// do something specific
}
This is somewhat similar in structure to the main use case for private virtual members. (Sort of. Not really. But it's what inspired me in this answer.)
You might also consider a type that is not MyObj, but implicitly converts to it, but the best way would be to refactor and perhaps extract the common generic something.
#include <iostream>
#include <boost/ref.hpp>
typedef int MyObj;
template <typename T>
struct foo {
void do_something(T obj) {
// do something generic...
std::cout << "generic " << obj << '\n';
}
};
template<>
void foo<MyObj>::do_something(MyObj obj) {
// do something specific...
std::cout << "special " << obj << '\n';
// and ALSO do something generic!
foo<boost::reference_wrapper<MyObj> >().do_something(boost::ref(obj));
}
int main()
{
foo<int> f;
f.do_something(10);
}
Yes, this this is actually quite straightforward. You just let the main, generic version of your function serve as a pass-through to an 'implementation' generic function which doesn't get partially specialized, then you can just call that from the specialized version of the initial function as needed.
template <typename T>
class foo
{
void do_something(T obj)
{
do_something_impl(obj);
}
void do_something_impl(T obj)
{
// do something generic...
}
};
Now the specialization can call the generic version without a problem:
template<>
void foo<MyObj>::do_something(MyObj obj)
{
// do something specific...
do_something_impl(obj); //The generic part
}
I think this closer to your original intentions than Steve M.'s answer, and is what I do when faced with this issue.
Related
I want to pass pointers of some functions to a template class to use them later. I was wondering if:
Does it make a possibly (speed-wise) beneficial difference if I make these functions inline?
Functions themselves are possibly one line wrapper for another functions like the example below:
//inline ?
void func_wrapper_1(){
func1(arg1);
}
//inline ?
void func_wrapper_2(){
func2(arg2);
}
and the class template is like the example below:
template<void(*f1)(), void(*f2)()>
class caller{
public:
static void func(int v){
if(v) {
(*f1)();
}else{
(*f2)();
}
}
};
And later on in the main function it will be used like the example below:
caller<func_wrapper_1,func_wrapper_2>::func(0);
caller<func_wrapper_1,func_wrapper_2>::func(1);
I know that every things depends on compiler and compiling option, but lets assume compiler accepts to make these functions inline.
Whether or not a compiler will be smart enough to inline a given situation is up for grabs but I think it may be possible by creating Callable Types by overloading the function call operator.
Something like this:
template<typename Func1, typename Func2>
class caller{
public:
static void func(int v){
if(v) {
// Func1() - creates an object of type Func1
// that object is 'called' using the '()' operator
Func1()();
}else{
Func2()();
}
}
};
struct CallableType1
{
// overloading the function call operator makes objects of
// this type callable
void operator()() const { std::cout << "callable 1" << '\n'; }
};
struct CallableType2
{
void operator()() const { std::cout << "callable 2" << '\n'; }
};
int main()
{
caller<CallableType1, CallableType2> cc;
cc.func(2);
}
Is it somehow possible to call every instantiation of a template function without knowing what will get instantiated at write-code-time?
#include <iostream>
template<typename T>
void print_size_of()
{
std::cout << sizeof(T) << "\n";
}
int main()
{
print_size_of<int>();
print_size_of<double>();
//won't work but maybe it shows what i want to do:
template<typename T>
print_size_of<T>();
//is there a syntax so that the compiler replaces that with `print_size_of<int>(); print_size_of<double>();`
}
This is possible; you need to add some static variable inside the function template body to record those instantiations.
In the code below, every instantiated function will have a static variable, whose constructor will register the function pointer to a global registration center:
std::vector<void(*)()> funcs;
struct helper {
explicit helper(void (*f)()) { funcs.push_back(f); }
};
template<typename T>
void print_size_of()
{
static helper _(&print_size_of<T>);
std::cout << sizeof(T) << "\n";
}
int main()
{
print_size_of<int>();
print_size_of<double>();
std::cout << "All instantiation:\n";
for ( auto f : funcs ) {
f();
}
}
EDIT:
This is not strictly recording instantiation. It only records those being called before. If you instantiate it by other methods like taking its address:
void (*f)() = &print_size_of<short>;
but don't call it, then this function pointer won't be registered.
EDIT2:
In fact, it is possible to faithfully record all instantiations. The key point is to associate the instantiation of the function template to the instantiation of a class template. Then a static member of that class will be guaranteed to initialize before entering main() function.
// this one is to make sure `funcs` is initialized
auto &get_funcs() {
static std::vector<void(*)()> funcs;
return funcs;
}
template<void (*f)()>
struct helper {
helper() { get_funcs().push_back(f); }
// this is a static class member, whose initialization is before main()
static helper _ins;
};
template<void (*f)()> helper<f> helper<f>::_ins;
template<typename T>
void print_size_of()
{
// force instantiation of a class
const void *_ = &helper<&print_size_of<T>>::_ins;
std::cout << sizeof(T) << "\n";
}
int main()
{
print_size_of<int>();
print_size_of<double>();
void (*f)() = &print_size_of<short>;
std::cout << "All instantiation:\n";
for ( auto f : get_funcs() ) {
f();
}
}
No, that is not possible.
You can get close by calling every instantiation which was already called once (Use a static variable to register on first call), but that's the best you can do.
So I've got a templatized class and I want to overload the behavior of a function when I have specific type, say char. For all other types, let them do their own thing. However, c++ won't let me overload the function.
Why can't I overload this function? I really really do not want to do template specialization, because then I've got duplicate the entire class.
Here is a toy example demonstrating the problem: http://codepad.org/eTgLG932
The same code posted here for your reading pleasure:
#include <iostream>
#include <cstdlib>
#include <string>
struct Bar
{
std::string blah() { return "blah"; }
};
template <typename T>
struct Foo
{
public:
std::string doX()
{
return m_getY(my_t);
}
private:
std::string m_getY(char* p_msg)
{
return std::string(p_msg);
}
std::string m_getY(T* p_msg)
{
return p_msg->blah();
}
T my_t;
};
int main(int, char**)
{
Foo<char> x;
Foo<Bar> y;
std::cout << "x " << x.doX() << std::endl;
return EXIT_SUCCESS;
}
Thank you everyone for your suggestions. Two valid solutions have been presented. I can either specialize the doX method, or specialize m_getY() method.
At the end of the day I prefer to keep my specializations private rather than public so I'm accepting Krill's answer.
You can specialize just one function out of an entire template class on a specific type without specializing the entire class. It would look like this:
template <> void Foo<char>::doX() {
/* custom implementation here */
}
You rarely see this in practice, but this is totally legal C++ code.
Hope this helps!
std::string m_getY(char* p_msg)
{
return std::string(p_msg);
}
std::string m_getY(T* p_msg)
{
return p_msg->blah();
}
The problem here is that when T is char, you end up with two functions with identical signature.
You can make your method m_getY a member function template. And them make specialization of this function template for char* or simply define a method with a char* argument. In this case you will not have to duplicate the whole class.
std::string m_getY(char* p_msg)
{
return std::string(p_msg);
}
template<typename U>
std::string m_getY(U* p_msg)
{
return p_msg->blah();
}
will not conflict with each other (and the former is not a specialisation of the latter, as it is not a template)
Is there a way in C++ to make an "untyed" function pointer ?
For example:
// pointer to global function
void foo( void (*fptr)() );
// pointer to member
void foo( void (Bar::*fptr)() );
Is there a way I can remove the class on which the member is ? So that I could do something like this:
void foo( void ("any type"::*fptr)(), "same type as for the pointer" &instance );
And then, in foo, I would like to store that pointer in a list, so that I can iterator over the list and call the function/member pointed to, regardless of what class it belongs to. Of course I'd need a list of instances on which to call the function.
Thx.
You can use a template.
template<typename T> void foo( void(T::*)(), T&) { ... }
However, people prefer to go for the function object approach. You can do this dynamically or statically.
void foo(std::function<void()> func) {
// std::bind is used to make this out of a member function
}
template<typename T> void foo(T t = T()) {
t(); // This is the best approach.
}
Edit: Some examples.
void foo(std::function<void()> func) {
std::cout << "In example one ";
func();
}
template<typename T> void foo(T t = T()) {
std::cout << "In example two ";
t();
}
class some_class {
public:
void func() { std::cout << "in ur function!\n"; }
};
int main(void)
{
some_class* ptr = NULL;
struct tempfunctor {
tempfunctor(some_class* newptr)
: ptr(newptr) {}
some_class* ptr;
void operator()() { return ptr->func(); }
};
foo(tempfunctor(ptr)); // Calls example two
foo(std::function<void()>(tempfunctor(ptr))); // Calls example one
foo(std::function<void()>(std::bind(&some_class::func, ptr)); // I'm not that familiar with bind, it looks something similar to this.
std::cin.get();
}
This is the idiom called the function object idiom, used heavily in STL and other high-quality libraries. The compile-time template is cleaner but the std::function can be bound at runtime.
Edit # OP: I didn't quite see your list requirement in there. A std::function<void()> is your best choice here.
The following seems to work fine with g++ and MSVC:
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <iostream>
using namespace std;
void foo( boost::function<int()> f )
{
cout << "f() = " << f() << endl;
}
template< class Type >
void foo( int (Type::*f)() const, Type const& o )
{
foo( boost::bind( f, boost::ref( o ) ) );
}
int func1() { return 1; }
struct S { int func2() const { return 2; } };
int main()
{
foo( func1 );
foo( &S::func2, S() );
}
Disclaimer: I seldom use the Boost stuff and I just typed the above without bothering to check the docs, so possibly it could be expressed more cleanly.
Also note that C++0x standard library offers the same functionality.
Cheers & hth.,
No. The bound class is an intrinsic part of the member function pointer type.
You can, however, use a member function pointer to a common baseclass, or a template.
Can you use functors in your list?
http://en.wikipedia.org/wiki/Function_object
Have a look at Fast Delegates: http://www.codeproject.com/KB/cpp/FastDelegate.aspx
This is an easy drop-in library that allows you to delegate pretty much anything and at a very high speed.
template <typename T>
void foo( void (T::*fptr)(), T& instance)
{
// ...
}
I'm not going to play expert here, but I think this will work, if not I would like to know why.
You can't have a pointer like that, but you could have a collection of boost::any, and put heterogeneous pointers (or any kind of functors) into it.
You can't do that, and you shouldn't do that even if you could, because it is against the spirit of the language. Create a base class with "fptr" as a pure virtual member, and inherit all your classes from that class.
consider this simple and pointless code.
#include <iostream>
struct A {
template<int N>
void test() {
std::cout << N << std::endl;
}
};
int main() {
A a;
a.test<1>();
}
It is a very simple example of a function template. What if however, I wanted to replace A::test with an overloaded operator() to make it a functor?
#include <iostream>
struct A {
template<int N>
void operator()() {
std::cout << N << std::endl;
}
};
int main() {
A a;
a<1>(); // <-- error, how do I do this?
}
Certainly if the operator() took parameters which were dependent on the template, the compiler could possibly deduce the template. But I just can't figure out the proper syntax to specify template parameters with a parameterless functor.
Is there a proper way to do this?
Obviously, this code would work since it bypasses the functor syntax:
a.operator()<1>();
but that kinda defeats the purpose of it being a functor :-P.
You can only call
a.operator()<1>();
but that would not be using a functor. Functors need a non template operator(), as they must be able to be called as varname() and that won't work with your code.
To make it a real functor change your code a template class (functors are classes):
#include <iostream>
template<int N>
struct A {
void operator()() {
std::cout << N << std::endl;
}
};
int main() {
A<1> a;
a();
}
There's not another "direct" way I know other than the:
a.operator()<1>();
syntax. If you're open to changing the code, moving the template parameter to the class would work, or using a (boost|tr1)::bind to make a (boost|tr1)::function object.
You are trying to pass a template parameter to an instance of an object, which as far as I know is not allowed. You can only pass templates parameters to template functions or template objects.
a.test<1>(); and a.operator()<1>(); work because they are serving as template functions.
Use boost::bind (check out boost libraries) to fix it though.
struct A {
void operator()(int n) {
std::cout << n << std::endl;
}
};
int main(int argc, char* argv[]) {
A a;
boost::function<void()> f = boost::bind<void>(a, 1);
f(); // prints 1
return 0;
}
And you don't even have to mess with templates!
You're stuck. Have you considered something like
struct A {
template<int N>
struct B
{
void operator()()
{ std::cout << N << std::endl; }
};
template<int N>
B<N> functor() {return B<N>();}
};
int main()
{
A a;
a.functor<1>()();
}
Nope, there's no way around it. Like you said, you have to either call the operator explicitly (which defeats the purpose), or the template arguments must be able to be deduced by the compiler.