I'm new at C++ and I'm trying to use find_if with templates but it doesn't seem to work the way I want it to. Why is that? I tried to find the answer in previous asked questions about templates with iterators, but I guess I missed the right one or maybe just didn't understand the answers correctly. I tried to use typename before iterator, but that didn't change the error-message.
Is there a better way to do this and if so, can someone help me to learn how to do this?
(error message: error C3867: 'UserInterface::Number': function call missing argument list, use '&Userinterface::Number' to create a pointer to member) =
When that happens, I know that I have missed () after the function call, but thats not the case this time?!
#include <iostream> // std::cout
#include <algorithm> // std::find_if
#include <vector> // std::vector
template<typename T>
class UserInterface
{
public:
bool Number(int i);
void function();
};
template<typename T>
bool UserInterface<T>::Number(int i) {
return (i >= 40);
}
template<typename T>
void UserInterface<T>::function()
{
std::vector<T> myvector;
myvector.push_back(10);
myvector.push_back(25);
myvector.push_back(15);
myvector.push_back(55);
myvector.push_back(1);
myvector.push_back(65);
myvector.push_back(40);
myvector.push_back(5);
std::vector<T>::iterator it = std::find_if(myvector.begin(), myvector.end(), Number);
std::cout << "The first value over 40 is " << *it << '\n';
std::cin.get();
}
int main() {
UserInterface<int> fu;
fu.function();
return 0;
}
There are a few problems with your example. The first is that std::find_if is incompatible with non-static member method pointers. Those pointers would require a this to work. Since UserInterface::Number doesn't access any non-static members and doesn't call any non-static methods, you can just make it static.
The second issue is that you must use & to obtain a pointer to your function.
Finally, don't forget typename before std::vector<T>::iterator.
#include <iostream> // std::cout
#include <algorithm> // std::find_if
#include <vector> // std::vector
template<typename T>
class UserInterface
{
public:
static bool Number(int i);
// ^^^^^^ Add static here
void function();
};
template<typename T>
bool UserInterface<T>::Number(int i) {
return (i >= 40);
}
template<typename T>
void UserInterface<T>::function()
{
std::vector<T> myvector;
myvector.push_back(10);
myvector.push_back(25);
myvector.push_back(15);
myvector.push_back(55);
myvector.push_back(1);
myvector.push_back(65);
myvector.push_back(40);
myvector.push_back(5);
typename std::vector<T>::iterator it =
// ^^^^^^^^ typename here
std::find_if(myvector.begin(), myvector.end(), &Number);
// ^
std::cout << "The first value over 40 is " << *it << '\n';
std::cin.get();
}
int main() {
UserInterface<int> fu;
fu.function();
return 0;
}
Related
This question already has answers here:
declaring a priority_queue in c++ with a custom comparator
(11 answers)
Closed 11 days ago.
I'm trying to make a program that has a std::priority_queue of objects. I want the queue to order the objects based on A::num.
This is the code:
#include <iostream>
#include <queue>
#include <vector>
class A {
public:
int num;
A (int n) {
this->num = n;
}
~A() {
std::cout << "Deleting an A\n";
}
};
struct Compare {
bool operator()(const A* first, const A* second) {
return first->num < second->num;
}
};
int main() {
std::priority_queue AContainer(A*, std::vector<A*>, Compare);
AContainer.push(new A(4));
AContainer.push(new A(8));
AContainer.push(new A(6));
while (AContainer.size() < 0) {
A* del = AContainer.top();
delete del;
del = nullptr;
AContainer.pop();
}
return 0;
}
The compiler returns an error, however I'm not sure why or where it is referring to, or how to fix it:
error: deduced class type 'priority_queue' in function return type
24 | std::priority_queue AContainer(A*, std::vector<A*>, Compare);
| ^~~~~~~~~~
In file included from /usr/include/c++/11/queue:64,
from /tmp/HvPOYonaOt.cpp:3:
/usr/include/c++/11/bits/stl_queue.h:456:11: note: 'template<class _Tp, class _Sequence, class _Compare> class std::priority_queue' declared here
456 | class priority_queue
| ^~~~~~~~~~~~~~
If you could help me out with this that would be great.
A*, std::vector<A*>, Compare are types that you need to supply to the template parameters of std::priority_queue, not values you supply to a constructor.
std::priority_queue<A*, std::vector<A*>, Compare> AContainer;
See it on coliru
Alternative fix using C++17 Class template argument deduction:
std::priority_queue AContainer{Compare{}, std::vector<A*>{}};
Which is closer to your example.
https://godbolt.org/z/547Tq1EnY
In this line you use invalid syntax for instantiating a templated class:
std::priority_queue AContainer(A*, std::vector<A*>, Compare);
The proper syntax is:
std::priority_queue<A*, std::vector<A*>, Compare> AContainer;
Having written this, in modern c++ it is recommended to avoid using raw pointers with manual new/delete whenever possible.
If you can simply store A instances this would be the simplest solution.
If you need pointers (e.g. for using polymorphism, which is not shown in your question), you can use smart pointers, e.g. std::unique_ptr:
#include <iostream>
#include <queue>
#include <vector>
class A {
public:
int num;
A(int n) {
this->num = n;
}
~A() {
std::cout << "Deleting an A\n";
}
};
struct Compare {
bool operator()(std::unique_ptr<A> const & first, std::unique_ptr<A> const & second) {
return first->num < second->num;
}
};
int main() {
std::priority_queue<std::unique_ptr<A>, std::vector<std::unique_ptr<A>>, Compare> AContainer;
AContainer.push(std::make_unique<A>(4));
AContainer.push(std::make_unique<A>(8));
AContainer.push(std::make_unique<A>(6));
while (AContainer.size() > 0) {
AContainer.pop();
}
return 0;
}
Note that you don't have to new or delete any object manually.
A side note: I think you have a typo in your while condition - while (AContainer.size() < 0) should be while (AContainer.size() > 0).
I have lots of code which creates a std::set<Port*>. Now by default this uses less<Port*> which sorts the set on pointer values. This creates problem in our tool. I want to provide a default comparator my_less for all set<Port*> without having to go and change all the code. So I tried template specialization :
namespace std {
template<> class set<Port*> : public set<Port*, my_less> {};
};
I put this code in a header file already included by all the code files. This works the way I expected that all set<Port*> are now using my_less. The problem is that such specialization is not inheriting constructors. So i get an error if I want to do this :
std::set<Port*> myset_new(myset1.begin(), myset1.end());
set.cpp:53:63: error: no matching function for call to
‘std::set<Port*>::set(std::set<Port*, znl_id_less>::iterator, std::set<Port*, my_less>::iterator)’
set_include.h:27:70: note: candidates are: std::set<Port*>::set()
set_include.h:27:70: note: std::set<Port*>::set(const std::set<Port*>&)
What is a good way to solve this (I can copy the code for constructors but that doesn't seem clean).
Is there any other way to achieve what I am trying to do apart from:
template<> class set<Port*> : public set<Port*, my_less> {};
Since C++11, you can inherit constructors easily with the using-declaration, like this:
namespace std {
template<> class set<Port*> : public set<Port*, my_less> {
public: using set<Port*, my_less>::set;
};
};
Here is my generic solutions to this problem
#include <set>
#include <functional>
#include <iostream>
// structs
struct my_struct
{
int a;
int b;
};
struct my_second_struct
{
int a;
int b;
};
bool operator<(const my_second_struct& m1, const my_second_struct& m2)
{
return m1.b < m2.b;
}
// alias helper
template <typename T>
using custom_comparator_t = std::function<bool(const T, const T)>;
// function to proxy already defined operator to pointers
template<typename T>
auto get_pointer_less()
{
return [](const T* v1, const T* v2) -> bool { return (*v1) < (*v2); };
}
// this is generic solution
template<typename T>
using pointer_set = std::set<T*, custom_comparator_t<T*>>;
// and final config
template<typename T>
auto get_pointer_set()
{
return pointer_set<T>( get_pointer_less<T>() );
}
template<typename T, typename _T>
void fill_and_display(_T& s)
{
s.insert(new T{10, 20});
s.insert(new T{20, 10});
s.insert(new T{0, 100});
s.insert(new T{100, 0});
std::cout << "set: ";
for (const T *ms : s)
std::cout << "( " << ms->a << " ; " << ms->b << " )";
std::cout << std::endl;
}
int main()
{
// First option, create custom less operator inline
std::set<my_struct *, custom_comparator_t<my_struct *>> myset(
[](const my_struct *ms1, const my_struct *ms2) { return ms1->a < ms2->a; }
);
fill_and_display<my_struct>(myset);
// Second option, reuse, already defined less operator
std::set<my_second_struct *, custom_comparator_t<my_second_struct*>> my_second_set(
get_pointer_less<my_second_struct>()
);
fill_and_display<my_second_struct>(my_second_set);
// generic colution for second set
auto my_third_set = get_pointer_set<my_second_struct>();
fill_and_display<my_second_struct>(my_third_set);
return 0;
}
I have a class with an accessor member function that I want to call and apply the result to a functor using std::for_each. I have a working version below that uses a for loop and for_each, but the for_each version is cryptic and cumbersome. Is there a way I can make the for_each version more concise, considering I have access to boost, but not C++11?
#if 0
// for loop version:
for(value_vector_type::iterator it = values.begin(); it!=values.end(); it++){
avg(it->getValue()); // I want to put this in a for_each loop
}
#else
// bind version:
std::for_each(values.begin(), values.end(), // iterate over all values
boost::bind(
boost::mem_fn(&average_type::operator()), // attach the averaging functor to the output of the getvalue call
&avg,
boost::bind(
boost::mem_fn(&value_wrapper_type::getValue), // bind the getValue call to each element in values
_1
)
)
);
#endif
Here is the full working implementation:
#include <vector>
#include <algorithm>
#include <iostream>
#include <boost/bind.hpp>
#include <boost/bind/mem_fn.hpp>
// A value wrapper
template<typename T>
struct Value {
Value(){}
Value(const T& value, bool valid = true):m_value(value),m_valid(valid){}
T getValue(){ return m_value; }
bool getValid(){ return m_valid; }
void setValue(const T& value){ m_value = value; }
void setValid(const T& valid){ m_valid = valid; }
private:
T m_value;
bool m_valid;
};
// Class that calculates the average piecewise
template<typename T>
struct Average {
private:
T m_numPoints;
T m_ChannelSum;
public:
Average() : m_numPoints(0), m_ChannelSum(0.0){}
void operator()(T value){
m_numPoints++;
m_ChannelSum+=value;
}
double getAverage(){ return m_ChannelSum/m_numPoints; }
T getCount(){ return m_numPoints; }
T getSum(){ return m_ChannelSum; }
};
// Run the average computation on several values
int main(int argc, char** argv){
typedef int value_type;
typedef Value<value_type> value_wrapper_type;
typedef std::vector<value_wrapper_type> value_vector_type;
value_vector_type values;
values.push_back(value_wrapper_type(5));
values.push_back(value_wrapper_type(7));
values.push_back(value_wrapper_type(3));
values.push_back(value_wrapper_type(1));
values.push_back(value_wrapper_type(2));
typedef Average<value_type> average_type;
average_type avg;
#if 0
// for loop version:
for(value_vector_type::iterator it = values.begin(); it!=values.end(); it++){
avg(it->getValue()); // I want to put this in a for_each loop
}
#else
// bind version:
std::for_each(values.begin(), values.end(), // iterate over all values
boost::bind(
boost::mem_fn(&average_type::operator()), // attach the averaging functor to the output of the getvalue call
&avg,
boost::bind(
boost::mem_fn(&value_wrapper_type::getValue), // bind the getValue call to each element in values
_1
)
)
);
#endif
std::cout << "Average: " << avg.getAverage() << " Count: " << avg.getCount() << " Sum: " << avg.getSum() << std::endl;
}
note: my original question was how to construct a for_each at all, but I've found that solution and a whole new question did not make much sense.
Thanks, all help is really appreciated!
If you don't have C++11 but Boost you could try a bind() expression (which would also work with C++2011 as bind() is part of C++2011):
std::for_each(a.begin(), a.end(), bind(&avg<value_type>, bind(&Value<value_type>::getValue, _1)));
if you are using c++11 then you can try
for(auto& a: values)
avg(a->getValue());
or
std::for_each(a.begin(), a.end(), [](whatever_type& wt){
avg(wt->getValue());
});
If you are not, then I think that toy have as good as your going to get although formatting wont hurt.
for(value_vector_type::iterator it = values.begin();
it!=values.end();
++it)
{
avg(it.getValue()); // I want to put this in a for_each loop
}
Trying to be too clever with function object and the like can often have the inverse effect of obscuring your code.
One way to make it look neater is to use Boost.Phoenix. You can shorten down to this:
std::for_each(values.begin(), values.end(), lazy(avg)(arg1.getValue()));
Heres how to do that. First thing you need to do is make the avg function object lazy. The simplest way to that is in-place with a function, defined like this:
template<class Function>
function<Function> lazy(Function x)
{
return function<Function>(x);
}
Next thing you need to do is write a function object for getValue, that can be lazy, like this:
struct get_value_impl
{
// result_of protocol:
template <typename Sig>
struct result;
template <typename This, typename T>
struct result<This(Value<T>&)>
{
// The result will be T
typedef typename T type;
};
template <typename V>
typename result<get_value_impl(V &)>::type
operator()(V & value) const
{
return value.getValue();
}
};
Thirdly, we extend the phoenix actors, using our get_value_impl class, so it will have a getValue method, like this:
template <typename Expr>
struct value_actor
: actor<Expr>
{
typedef actor<Expr> base_type;
typedef value_actor<Expr> that_type;
value_actor( base_type const& base )
: base_type( base ) {}
typename expression::function<get_value_impl, that_type>::type const
getValue() const
{
function<get_value_impl> const f = get_value_impl();
return f(*this);
}
};
Finally, we put it all together by defining the argument and passing it into the for_each algorithm:
expression::terminal<phoenix::argument<1>, value_actor> arg1;
std::for_each(values.begin(), values.end(), lazy(avg)(arg1.getValue()));
Credit goes to Mathias Gaunard on the boost.users mailing list for pointing me towards this solution:
std::for_each(values.begin(), values.end(),
boost::bind(boost::ref(avg), boost::bind(&value_wrapper_type::getValue, _1))
);
Wrapping avg with boost::ref is required because otherwise a copy of avg is filled out with the results of getValue(), rather than avg itself.
Here is the full compiled and tested solution:
#include <stdexcept>
#include <vector>
#include <algorithm>
#include <iostream>
#include <boost/bind.hpp>
#include <boost/bind/mem_fn.hpp>
// A value wrapper
template<typename T>
struct Value {
Value(){}
Value(const T& value, bool valid = true):m_value(value),m_valid(valid){}
T getValue(){ return m_value; }
bool getValid(){ return m_valid; }
void setValue(const T& value){ m_value = value; }
void setValid(const T& valid){ m_valid = valid; }
private:
T m_value;
bool m_valid;
};
// Class that calculates the average piecewise
template<typename T>
struct Average {
private:
T m_numPoints;
T m_ChannelSum;
public:
typedef void result_type;
Average() : m_numPoints(0), m_ChannelSum(0.0){}
result_type operator()(T value){
m_numPoints++;
m_ChannelSum+=value;
}
double getAverage(){
if (m_ChannelSum==0) {
throw std::logic_error("Cannot get average of zero values");
}
return m_ChannelSum/m_numPoints;
}
T getCount(){ return m_numPoints; }
T getSum(){ return m_ChannelSum; }
};
// Run the average computation on several values
int main(int argc, char** argv){
typedef int value_type;
typedef Value<value_type> value_wrapper_type;
typedef std::vector<value_wrapper_type> value_vector_type;
value_vector_type values;
values.push_back(value_wrapper_type(5));
values.push_back(value_wrapper_type(7));
values.push_back(value_wrapper_type(3));
values.push_back(value_wrapper_type(1));
values.push_back(value_wrapper_type(2));
typedef Average<value_type> average_type;
average_type avg;
#if 0
// for loop version:
for(value_vector_type::iterator it = values.begin(); it!=values.end(); it++){
avg(it->getValue()); // I want to put this in a for_each loop
}
#else
// bind version:
std::for_each(values.begin(), values.end(),
boost::bind(boost::ref(avg), boost::bind(&value_wrapper_type::getValue, _1))
);
#endif
std::cout << "Average: " << avg.getAverage() << " Count: " << avg.getCount() << " Sum: " << avg.getSum() << std::endl;
}
If you can use boost, but not C++11 features, then I would consider using the BOOST_FOREACH macro
Yes, it's a macro, but as macros go it's well behaved
It's also reads quite nicely and is hard to get wrong
BOOST_FOREACH(const Value& rValue, values)
{
avg(rValue.getValue());
}
C++11 range based for loops will replace it
I have some generic code for deleting pointers within a vector or a value of a Map.
Is there a better way of doing this (without using shared_ptrs or any o fthe tr1 extensions )?
Also is the code correct?
Here is my code:
I have a namespace
#ifndef CONTAINERDELETE_H
#define CONTAINERDELETE_H
#include <functional>
#include <map>
#include <vector>
#include <algorithm>
namspace ContainerDelete{
template<class A, class B>
struct DeleteMap
{
bool operator()( pair<A,B> &x) const
{
delete x.second;
return true;
}
};
template<class T>
struct DeleteVector
{
bool operator()(T &x) const
{
delete x;
return true;
}
};
}
#endif
I would then use this namespace in some bit of code to delete a map or vector.
Test Map deletion.
#include "ContainerDelete.h"
using namespace std;
// Test function.
void TestMapDeletion()
{
// Add 10 string to map.
map<int,B*> testMap;
for( int Idx = 0; Idx < 10; ++Idx )
{
testMap[Idx] = new B();
}
// Now delete the map in a single line.
for_each( testMap.begin(),
testMap.end(),
ContainerDelete::DeleteMap<int,B*>());
}
Test Vector Deletion
// Test Function.
void TestVectorDeletion()
{
// Add 10 string to vector.
vector<B*> testVector;
for( int Index = 0; Index < 10; ++Index )
{
testVector.push_back( new B());
}
// Now delete the vector in a single line.
for_each( testVector.begin(),
testVector.end(),
ContainerDelete::DeleteVector<B*>());
}
Thanks,
Mike
Better would be if reduce the genericity as:
struct DeleteVector
{
template<class T> //use the template here!
void operator()(T &x) const
{
delete x;
}
};
if you do so, then you could simply write this:
for_each(testVector.begin(),
testVector.end(),
ContainerDelete::DeleteVector());
No need to pass type argument when you use DeleteVector, for it is not a class template anymore!
Similarly, you can implement DeleteMap functor.
You should also rename DeleteVector to DeleteT, and DeleteMap to DeletePairSecond, as both of these can be used more generically. For example, DeleteT can be used even with std::list, or even with arrays.
The code is ok. I can't imagine any other ways to delete the pointers. All you can do is to reduce explicit type specification like in upper question. I know one more uglier way to do it: functions deduce types of their template parameters. So you can write template function with the first argument - vector, second - ptr and then use std::bind of vector parameter to make this function accepting one parameter - ptr.
But functor is better and more flexible.
I developed a scripting engine that has many built-in functions, so to call any function, my code just went into an if .. else if .. else if wall checking the name but I would like to develop a more efficient solution.
Should I use a hashmap with strings as keys and pointers as values? How could I do it by using an STL map?
EDIT:
Another point that came into my mind: of course using a map will force the compiler not to inline functions, but my inefficient approach didn't have any overhead generated by the necessity of function calls, it just executes code.
So I wonder if the overhead generated by the function call will be any better than having an if..else chain.. otherwise I could minimize the number of comparisons by checking a character at runtime (will be longer but faster).
Whatever your function signatures are:
typedef void (*ScriptFunction)(void); // function pointer type
typedef std::unordered_map<std::string, ScriptFunction> script_map;
// ...
void some_function()
{
}
// ...
script_map m;
m.emplace("blah", &some_function);
// ...
void call_script(const std::string& pFunction)
{
auto iter = m.find(pFunction);
if (iter == m.end())
{
// not found
}
(*iter->second)();
}
Note that the ScriptFunction type could be generalized to std::function</* whatever*/> so you can support any callable thing, not just exactly function pointers.
In C++11 you can do something like this :
This Interface needs only the return type and it takes care of everything else from the caller side.
#include <string>
#include <iostream>
#include <map>
#include <vector>
#include <typeinfo>
#include <typeindex>
#include <cassert>
void fun1(void){
std::cout<<"inside fun1\n";
}
int fun2(){
std::cout<<"inside fun2\n";
return 2;
}
int fun3(int a){
std::cout<<"inside fun3\n";
return a;
}
std::vector<int> fun4(){
std::cout<<"inside fun4\n";
std::vector<int> v(4,100);
return v;
}
// every function pointer will be stored as this type
typedef void (*voidFunctionType)(void);
struct Interface{
std::map<std::string,std::pair<voidFunctionType,std::type_index>> m1;
template<typename T>
void insert(std::string s1, T f1){
auto tt = std::type_index(typeid(f1));
m1.insert(std::make_pair(s1,
std::make_pair((voidFunctionType)f1,tt)));
}
template<typename T,typename... Args>
T searchAndCall(std::string s1, Args&&... args){
auto mapIter = m1.find(s1);
/*chk if not end*/
auto mapVal = mapIter->second;
// auto typeCastedFun = reinterpret_cast<T(*)(Args ...)>(mapVal.first);
auto typeCastedFun = (T(*)(Args ...))(mapVal.first);
//compare the types is equal or not
assert(mapVal.second == std::type_index(typeid(typeCastedFun)));
return typeCastedFun(std::forward<Args>(args)...);
}
};
int main(){
Interface a1;
a1.insert("fun1",fun1);
a1.insert("fun2",fun2);
a1.insert("fun3",fun3);
a1.insert("fun4",fun4);
a1.searchAndCall<void>("fun1");
int retVal = a1.searchAndCall<int>("fun3",2);
a1.searchAndCall<int>("fun2");
auto temp = a1.searchAndCall<std::vector<int>>("fun4");
return 0;
}
You can also use Boost.Function and Boost.Bind what even allows you, to some degree, to have map of heterogeneous functions:
typedef boost::function<void, void> fun_t;
typedef std::map<std::string, fun_t> funs_t;
funs_t f;
void foo() {}
void goo(std::string& p) {}
void bar(int& p) {}
f["foo"] = foo;
f["goo"] = boost::bind(goo, "I am goo");
f["bar"] = boost::bind(bar, int(17));
It can be a map of functions of compatible prototypes as well, of course.
Above answers seem to give a complete overview, this regards only your second question:
Map element retrieval by key has O(log n) complexity. Hashmap retrieval by key has O(1) complexity + a little stuff on the side in case of collisions. So if theres a good hash function for your function names, use it. Your implementation will have a standard one. It should be fine.
But be aware, that anything below a hundred elements will not benefit all too much.
The only downside of a hash map is collision. In your case, the hashmap will be relatively static. You know the function names you support. So I advise you to create a simple test case, where you call unordered_map<...>::hash_function with all your keys to make sure that nothing collides. After that, you can forget about it.
A quick google for potential improvements on hash functions got me there:
A fiew good hash functions
Maybe, depending on your naming conventions, you can improve on some aspects of the function.
Well, you can use any_map to store functions with different signatures (but calling it will be messy) and you can use int_map to call functions with a specific signature (looks nicer).
int FuncA()
{
return 1;
}
float FuncB()
{
return 2;
}
int main()
{
// Int map
map<string,int(*)()> int_map;
int_map["A"] = FuncA;
// Call it
cout<<int_map["A"]()<<endl;
// Add it to your map
map<string, void(*)> any_map;
any_map["A"] = FuncA;
any_map["B"] = FuncB;
// Call
cout<<reinterpret_cast<float(*)()>(any_map["B"])()<<endl;
}
I've managed to modify the example from Mohit to work on member function pointers:
#include <string>
#include <iostream>
#include <map>
#include <vector>
#include <typeinfo>
#include <typeindex>
#include <cassert>
template <typename A>
using voidFunctionType = void (A::*)(void);
template <typename A>
struct Interface{
std::map<std::string,std::pair<voidFunctionType<A>,std::type_index>> m1;
template<typename T>
void insert(std::string s1, T f1){
auto tt = std::type_index(typeid(f1));
m1.insert(std::make_pair(s1,
std::make_pair((voidFunctionType<A>)f1,tt)));
}
template<typename T,typename... Args>
T searchAndCall(A a, std::string s1, Args&&... args){
auto mapIter = m1.find(s1);
auto mapVal = mapIter->second;
auto typeCastedFun = (T(A::*)(Args ...))(mapVal.first);
assert(mapVal.second == std::type_index(typeid(typeCastedFun)));
return (a.*typeCastedFun)(std::forward<Args>(args)...);
}
};
class someclass {
public:
void fun1(void);
int fun2();
int fun3(int a);
std::vector<int> fun4();
};
void someclass::fun1(void){
std::cout<<"inside fun1\n";
}
int someclass::fun2(){
std::cout<<"inside fun2\n";
return 2;
}
int someclass::fun3(int a){
std::cout<<"inside fun3\n";
return a;
}
std::vector<int> someclass::fun4(){
std::cout<<"inside fun4\n";
std::vector<int> v(4,100);
return v;
}
int main(){
Interface<someclass> a1;
a1.insert("fun3",&someclass::fun3);
someclass s;
int retVal = a1.searchAndCall<int>(s, "fun3", 3);
return 0;
}