How is type deduced from auto return type?

Viewed 954

This answer has a code snippet like this :

template<class T, class F>
auto f(std::vector<T> v, F fun)
    -> decltype( bool( fun(v[0] ) ), void() )
{
  // ...
}

It really compiles and work (at least on Ideone).

So, how is the type deduced in this case?

Is next line really allowed by c++11 standard?

decltype( bool( fun(v[0] ) ), void() )

I took a quick look, and it doesn't look valid. Is ideone wrong in this case?


All examples in the c++11 standard are such that they all got only one type in the decltype :

struct A {
  char g();
  template<class T> auto f(T t) -> decltype(t + g())
  { return t + g(); }
};

another example :

void f3() {
  float x, &r = x;
  [=] {
  decltype(x) y1;
  decltype((x)) y2 = y1;
  decltype(r) r1 = y1;
  decltype((r)) r2 = y2;
};

and another

const int&& foo();
int i;
struct A { double x; };
const A* a = new A();
decltype(foo()) x1 = i;
decltype(i) x2;
decltype(a->x) x3;
decltype((a->x)) x4 = x3;

They all got only one parameter in decltype. How come the top code take two parameters (separated by a comma)?


I created another example (which fails to compile) :

#include <vector>
#include <iostream>

template<class T, class F>
auto f(std::vector<T> v, F fun) -> decltype(bool(fun(v[0])), void())
{
  // ...
  (void)v;(void)fun;

  return fun(v.size());
}

void ops(int)
{
}

int main(){
  std::vector<int> v;
  f(v, [](int){ return true; });
  f(v,ops);
}

Even if the line f(v,ops); is removed, the return type of the f template function is evaluated to void.

2 Answers
Related