Сообщение Re: Ответ на часть 2 от 04.09.2015 13:26
Изменено 04.09.2015 13:32 SaZ
Собственно решение (ideone.com/cQEMM4):
#include <iostream>
#include <string>
template< typename T >
class Invoker
{
public:
Invoker() = default;
virtual ~Invoker() = default;
public:
template <typename R, typename ... Args1, typename ... Args2>
static R invoke( T* object, R( T::* method )( Args1... ), Args2&&... args );
};
template <typename T>
template <typename R, typename ... Args1, typename ... Args2>
R Invoker<T>::invoke(T* object, R( T::* method)(Args1...), Args2&&... args)
{
return ( object->*method )( std::forward<Args1>( args )... );
}
struct Foo
{
int bar( const std::string& txt )
{
return 42;
}
};
int main()
{
Foo foo;
auto result = Invoker<Foo>::invoke( &foo, &Foo::bar, std::string() );
std::cout << result; // 42
return 0;
}
Re: Ответ на часть 2
Собственно решение (ideone.com/cQEMM4):
З.Ы, как я понимаю, костыльное. Я пока плохо представляю, что такое rvalue references, но учусь.
#include <iostream>
#include <string>
template< typename T >
class Invoker
{
public:
Invoker() = default;
virtual ~Invoker() = default;
public:
template <typename R, typename ... Args1, typename ... Args2>
static R invoke( T* object, R( T::* method )( Args1... ), Args2&&... args );
};
template <typename T>
template <typename R, typename ... Args1, typename ... Args2>
R Invoker<T>::invoke(T* object, R( T::* method)(Args1...), Args2&&... args)
{
return ( object->*method )( std::forward<Args1>( args )... );
}
struct Foo
{
int bar( const std::string& txt )
{
return 42;
}
};
int main()
{
Foo foo;
auto result = Invoker<Foo>::invoke( &foo, &Foo::bar, std::string() );
std::cout << result; // 42
return 0;
}
З.Ы, как я понимаю, костыльное. Я пока плохо представляю, что такое rvalue references, но учусь.