How do I say:
template<typename T>
class X {
// if T has method x(), define
// Y x() { return t.x() }
T t;
};
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Just define it.
If
X::xisn’t called, thenT::xdoesn’t have to exist either. IfX::xis called andT::xdoesn’t exist, the error message will point to the use ofX::x. Most compilers would use wording along the lines of: “Unknown identifierxwhile compilingY X<Something>::x(void)within this context: whatever called X::x() for a Something that doesn’t support it“.EDIT: Since you’re using C++0x, by all means use decltype:
I’m not 100% sure about whether to use
decltype(T::x()),decltype(t.x()), ordecltype(this->t.x()), but I’m pretty sure this should work. Iftdoesn’t supplyx, then theForwards::x()function wouldn’t be able to be instantiated. This still isn’t perfect forwarding, since you need to know the argument list a-priori, but now you can deal with return type variation.