top | item 19657989

(no title)

net4all | 6 years ago

In the example the return type of add does not depend on the value of the arguments. So it is not dependent types.

discuss

order

Davidbrcz|6 years ago

In this piece of code, the return type is explicitly dependent of the type of the argument (it's path related template

  #include <iostream>
  #include <string>
  
  struct A {using ret = std::string;};
  struct B {using ret = int;};
  
  
  template <class T>
  auto f(T t) -> typename T::ret
  {
    return {};
  }
  
  int main(int argc, char *argv[])
  {
    auto s = f(A{});
    std::cout << s.size() << "\n";
    auto i = f(B{});
    std::cout << i+1 << "\n";
    return 0;
  }
There is also stuff like this where the return type is defined as the return type of builder.makeObject(), builder which is the argument:

  template <typename Builder>
  auto
  makeAndProcessObject (const Builder& builder) -> decltype(builder.makeObject() )
  {
      auto val = builder.makeObject();
      // do stuff with val
      return val;
  }

Retra|6 years ago

The return type has to be dependent on the value of the input, not its type. Like it'll return an int[3] if you provide 3 as in input and int[4] if you provide 4. Note that both 3 and 4 should have the same type, but int[3] and int[4] are different types.