Thursday 15 March 2012

c++ - How to get the return type of a member function from within a class? -



c++ - How to get the return type of a member function from within a class? -

the next programme yields compilation error clang, though passes on other compilers:

#include <utility> struct foo { auto bar() -> decltype(0) { homecoming 0; } using bar_type = decltype(std::declval<foo>().bar()); }; int main() { homecoming 0; }

clang yields:

$ clang -std=c++11 clang_repro.cpp clang_repro.cpp:10:48: error: fellow member access incomplete type 'foo' using bar_type = decltype(std::declval<foo>().bar()); ^ clang_repro.cpp:3:8: note: definition of 'foo' not finish until closing '}' struct foo ^ 1 error generated.

is programme illegal, , if so, there right way define foo::bar_type?

clang details:

$ clang --version ubuntu clang version 3.5-1ubuntu1 (trunk) (based on llvm 3.5) target: x86_64-pc-linux-gnu thread model: posix

g++4.9 issues same error

i'm not sure if invalid code, because incomplete types allowed declval, , look in decltype not evaluated. rightføld in answer explained why code invalid.

you can utilize std::result_of:

using bar_type = std::result_of<decltype(&foo::bar)(foo)>::type;

which implemented this:

using bar_type = decltype((std::declval<foo>().*std::declval<decltype(&foo::bar)>())());

the difference between , code in question pointer-to-member operator (.*) used instead of fellow member access operator (.), , doesn't require type complete, demonstrated code:

#include <utility> struct foo; int main() { int (foo::*pbar)(); using bar_type = decltype((std::declval<foo>().*pbar)()); }

c++ clang return-type-deduction

No comments:

Post a Comment