C++ Guru Question

Wondering about this… 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(T)> fn)
{
  return fn(foo.m_t);
}

void mystery()
{
  auto foo = Foo{1};

  // this works...
  function(int)> f1 = [] (int i) { return Foo{i}; };
  auto bar1 = foo /= f1;

  // this doesn't
  auto f2 = [] (int i) { return Foo{i}; };
  auto bar2 = foo /= f2;

  // clang++ 3.3-16ubuntu1 says:
  // file:line:col: error: no viable overloaded '/='
  // auto bar2 = foo /= f2;
  //             ~~~ ^  ~~
  
  // file:line:col: note: candidate template ignored: could not match
  // 'function (type-parameter-0-0)>' against
  // ''
  // Foo operator/=(Foo foo, std::function(T)> fn)
  //        ^
}

(followup to this)

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.