{"id":912,"date":"2014-08-11T23:23:32","date_gmt":"2014-08-12T06:23:32","guid":{"rendered":"http:\/\/www.elbeno.com\/blog\/?p=912"},"modified":"2015-06-30T21:18:28","modified_gmt":"2015-07-01T04:18:28","slug":"c-guru-question","status":"publish","type":"post","link":"https:\/\/www.elbeno.com\/blog\/?p=912","title":{"rendered":"C++ Guru Question"},"content":{"rendered":"<p>Wondering about this&#8230; template argument deduction succeeds for the explicitly-typed variable, fails in the auto case. (Also, it succeeds either way for an equivalently-typed unary operator template).<\/p>\n<pre lang=\"cpp\">\r\ntemplate <typename T>\r\nstruct Foo\r\n{\r\n  T m_t;\r\n};\r\n\r\ntemplate <typename T, typename U>\r\nFoo<U> operator\/=(Foo<T> foo, function<Foo<U>(T)> fn)\r\n{\r\n  return fn(foo.m_t);\r\n}\r\n\r\nvoid mystery()\r\n{\r\n  auto foo = Foo<int>{1};\r\n\r\n  \/\/ this works...\r\n  function<Foo<int>(int)> f1 = [] (int i) { return Foo<int>{i}; };\r\n  auto bar1 = foo \/= f1;\r\n\r\n  \/\/ this doesn't\r\n  auto f2 = [] (int i) { return Foo<int>{i}; };\r\n  auto bar2 = foo \/= f2;\r\n\r\n  \/\/ clang++ 3.3-16ubuntu1 says:\r\n  \/\/ file:line:col: error: no viable overloaded '\/='\r\n  \/\/ auto bar2 = foo \/= f2;\r\n  \/\/             ~~~ ^  ~~\r\n  \r\n  \/\/ file:line:col: note: candidate template ignored: could not match\r\n  \/\/ 'function<Foo<type-parameter-0-1> (type-parameter-0-0)>' against\r\n  \/\/ '<lambda at file:line:col>'\r\n  \/\/ Foo<U> operator\/=(Foo<T> foo, std::function<Foo<U>(T)> fn)\r\n  \/\/        ^\r\n}\r\n<\/pre>\n<p>(<a href=\"https:\/\/www.elbeno.com\/blog\/?p=924\">followup to this<\/a>)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Wondering about this&#8230; template argument deduction succeeds for the explicitly-typed variable, fails in the auto case. (Also, it succeeds either way for an equivalently-typed unary operator template). template struct Foo { T m_t; }; template Foo operator\/=(Foo foo, function fn) { return fn(foo.m_t); } void mystery() { auto foo =&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22,8],"tags":[],"class_list":["post-912","post","type-post","status-publish","format-standard","hentry","category-cpp","category-programming"],"_links":{"self":[{"href":"https:\/\/www.elbeno.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/912","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.elbeno.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.elbeno.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.elbeno.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.elbeno.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=912"}],"version-history":[{"count":12,"href":"https:\/\/www.elbeno.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/912\/revisions"}],"predecessor-version":[{"id":929,"href":"https:\/\/www.elbeno.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/912\/revisions\/929"}],"wp:attachment":[{"href":"https:\/\/www.elbeno.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=912"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.elbeno.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=912"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.elbeno.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=912"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}