#include <string>
// a type which yields the type we gave it
template<class T> struct passer
{
using type = T;
};
// an easy-to-use alias
template<class T> using pass_t = typename passer<T>::type;
// example
template <typename Inner>
class Outer {
private:
Inner inner;
public:
// To-do: replicate foo method of Inner with identical signature,
// how to pick correct T?
// Ans: with a pass_t
template<class T>
auto foo(T&& arg)
-> pass_t<decltype(this->inner.foo(std::forward<T>(arg)))>
{
return inner.foo(std::forward<T>(arg));
}
};
struct Bar
{
void foo(std::string const& thing);
};
struct Baz
{
int foo(int thing) { return thing * 2; };
};
int main()
{
auto o = Outer<Bar>();
o.foo(std::string("hi"));
o.foo("hi");
int i = 1;
/* - uncomment for error
o.foo(i);
note the nice error message on gcc:
<source>:41:7: error: no matching member function for call to 'foo'
<source>:19:10: note: candidate template ignored: substitution failure [with T = int]: reference to type 'const std::string' (aka 'const basic_string<char>') could not bind to an lvalue of type 'int'
*/
// same here:
// o.foo(1);
// but this is fine
auto o2 = Outer<Baz>();
auto x = o2.foo(2);
// and this is not
// note: candidate template ignored: substitution failure [with T = char const (&)[6]]: cannot initialize a parameter of type 'int' with an lvalue of type 'char const[6]'
// auto y = o2.foo("dfghj");
}
此处链接:
https://godbolt.org/g/UvsrbP