{"id":45044,"date":"2015-05-25T07:00:00","date_gmt":"2015-05-25T21:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2015\/05\/25\/new-c-experimental-feature-the-tadpole-operators\/"},"modified":"2025-12-22T19:24:36","modified_gmt":"2025-12-23T03:24:36","slug":"20150525-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20150525-00\/?p=45044","title":{"rendered":"New C++ experimental feature: The tadpole operators"},"content":{"rendered":"<p>How often have you had to write code like this:<\/p>\n<pre>x = (y + 1) % 10;\r\nx = (y + 1) * (z - 1);\r\nx = (wcslen(s) + 1) * sizeof(wchar_t);\r\n<\/pre>\n<p>Since the <code>+<\/code> and <code>-<\/code> operators have such low precedence, you end up having to parenthesize them a lot, which can lead to heavily nested code that is hard to read.<\/p>\n<p>Visual Studio 2015 RC contains a pair of experimental operators, nicknamed tadpole operators. They let you add and subtract one from an integer value without needing parentheses.<\/p>\n<pre>x = -~y % 10;\r\nx = -~y * ~-z;\r\nx = -~wcslen(s) * sizeof(wchar_t);\r\n<\/pre>\n<p>They&#8217;re called tadpole operators because they look like a tadpole swimming toward or away from the value. The tilde is the tadpole&#8217;s head and the hyphen is the tail.<\/p>\n<table style=\"border-collapse: collapse;\" border=\"1\" cellpadding=\"3\">\n<tbody>\n<tr>\n<th>Syntax<\/th>\n<th>Meaning<\/th>\n<th>Mnemonic<\/th>\n<\/tr>\n<tr>\n<td align=\"center\"><code>-~y<\/code><\/td>\n<td align=\"center\"><code>y + 1<\/code><\/td>\n<td>Tadpole swimming toward a value makes it bigger<\/td>\n<\/tr>\n<tr>\n<td align=\"center\"><code>~-y<\/code><\/td>\n<td align=\"center\"><code>y - 1<\/code><\/td>\n<td>Tadpole swimming away from a value makes it smaller<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>To enable the experimental tadpole operators, add this line to the top of your C++ file<\/p>\n<pre>#define __ENABLE_EXPERIMENTAL_TADPOLE_OPERATORS\r\n<\/pre>\n<p>For example, here&#8217;s a simple program that illustrates the tadpole operators.<\/p>\n<pre>#define __ENABLE_EXPERIMENTAL_TADPOLE_OPERATORS \r\n#include &lt;ios&gt;\r\n#include &lt;iostream&gt;\r\n#include &lt;istream&gt;\r\n \r\nint __cdecl main(int, char**)\r\n{\r\n   int n = 3;\r\n   std::cout &lt;&lt; \"3 + 1 = \" &lt;&lt; -~n &lt;&lt; std::endl;\r\n   std::cout &lt;&lt; \"(3 - 1) * (3 + 1) \" &lt;&lt; ~-n * -~n &lt;&lt; std::endl;\r\n   return 0;\r\n}\r\n<\/pre>\n<p>Remember that these operators are still experimental. They are not officially part of C++, but you can play with them and <span style=\"text-decoration: line-through;\">give your feedback here<\/span> <a title=\"The tadpole operators explained\" href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20150526-00\/?p=45034\"> learn more about them here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Give it a whirl.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-45044","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Give it a whirl.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/45044","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=45044"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/45044\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=45044"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=45044"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=45044"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}