Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
358 views
in Technique[技术] by (71.8m points)

c++ - Enlightening Usage of C++11 decltype

I've just seen this really nice talk Rock Hard: C++ Evolving by Boris Jabes. In the section of the talk concerning Higher-Order Generic Programming he says that the following is an example of a function that is more generic with regards to its return type and leads to fewer template function overloads

template <typename Func>
auto deduce(const Func & f) -> decltype(f())
{..}

This however can be realized using plain template syntax as follows

template <typename Func>
Func deduce(const Func & f)
{..}

so I guess the example chosen doesn't really show the unique power of decltype. Can anyone give an example of such a more enlightening usage of decltype?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your suspicions are incorrect.

void f() { }

Now deduce(&f) has type void, but with your rewrite, it has type void(*)(). In any case, everywhere you want to get the type of an expression or declaration, you use decltype (note the subtle difference in between these two. decltype(x) is not necessarily the same as decltype((x))).

For example, it's likely your Standard library implementation somewhere contains lines like

using size_t = decltype(sizeof(0));
using ptrdiff_t = decltype((int*)0 - (int*)0);
using nullptr_t = decltype(nullptr);

Finding out the correct return type of add has been a challenging problem throughout past C++. This is now an easy exercise.

template<typename A, typename B> 
auto add(A const& a, B const& b) -> decltype(a + b) { return a + b; }

Little known is that you can use decltype before :: and in a pseudo destructor name

// has no effect
(0).~decltype(0)();

// it and ite will be iterators into an initializer list 
auto x = { 1, 2, 3 };
decltype(x)::iterator it = x.begin(), ite = x.end();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...