Automatically detect template parameters without arguments

This is a response to a response to another SO message .

I have the following working code with the expected output.

#include <iostream>

template <typename T>
T twice(T in)
{
   return 2*in;
}

struct Foo
{
   Foo operator+(int (*func)(int in)) const
   {
      Foo ret{data};
      ret.data += func(ret.data);
      return ret;
   }
   int data;
};

int main()
{
   Foo f1{20};
   Foo f2 = f1 + twice;
   Foo f3 = f1 + twice<int>;
   std::cout << f2.data << std::endl;
   std::cout << f3.data << std::endl;
}

I did not know until yesterday that the compiler can output parameters such as a function template even without an argument. In the above code of expression

f1 + twice

and

f1 + twice<int>

lead to identical values.

My question is: where in the C ++ 03 / C ++ 11 standard can we find the necessary supporting documentation for the detection logic of an automatic compiler type?

+4
source share
2 answers

++ 11, , : http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3337.pdf.

, , Foo f2 = f1 + twice; twice . twice operator+. , :

14.8.2.2 [Temp.deduct.funcaddr]:

, (13.4). P A, , 14.8.2.5.

, funct. , 14.8.2.5 1 2.

14.8.2.5 [temp.deduct.type] `:

1 , , ( P), ( A), ( , - ), P ( A), .

2 P A, P A. P/A, . - P/A - , .

twice operator+, , operator+. , A, int (*)(int in) P of twice, A int twice(int).

, .

+3

++ 11 14.8.2.2 , .

, operator+, int (*)(int), twice, int . . 14.8.2.5, .

+4

Source: https://habr.com/ru/post/1545868/


All Articles