It's been a couple of years since I got the itch to write a technical article, but I recently found myself thinking about types in C++ and how the language sometimes adds or removes const without your explicitly asking it to do so. This is especially the case when it comes to new features in C++0x such as decltype and lambda expressions. The result is "Appearing and Disappearing consts in C++" at Dr. Dobbs. The normal online version of the article is available here, but I think the formatting they use makes it difficult to read, so I suggest you view the printer-formatted version of the article instead.
I hope you find the article interesting and the information in it useful.
Scott
Very interesting article. Thanks. I think I spotted a spelling mistake in the code though. Might want to fix this:
ReplyDeletedecltype(pcs-lgt;i) y;
I've forwarded this bug report (along with a couple of problems I spotted myself) to DDJ. Unfortunately, I was not given an opportunity to review their version of the article before they published it.
ReplyDeleteThe following is slightly off topic, but can I use decltype(*this) to ease the burden of writing something like
ReplyDeletetemplate<complex spec>
class C
{
typedef C<complex spec> this_type;
};
?
Well, "this" exists only inside a member function, so you can't use it at class scope as in your example. Also, *this is an lvalue, so I think it would have to look more like std::remove_reference<decltype(*this)>::type, but I'm not sure.
ReplyDeleteThe page says that "this article has been removed by the editors of Dr. Dobb's"!
ReplyDeleteSigh. There's a story here, but it deserves a blog entry of its own. I'll try to get that out tomorrow. At that time, I'll also provide a new link to the article. In the meantime, please be patient.
ReplyDelete