C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Function overload set type information loss

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Wed, 31 Jul 2024 19:01:43 -0400
On Wed, Jul 31, 2024 at 6:56 PM organicoman <organicoman_at_[hidden]> wrote:

> > This is called a mapping from int to int that means, if two functions stamped from the same template have the same mapping, then the same input gives the same output.
> > But with current C++ it is not the case.
> > Example:
> > template<int I>
> > int foo (int j)
> > {
> > return I * j;
> > }
> >
> > int main()
> > {
> > auto f1 = &foo<5>;
> > auto f2 = &foo<-3>;
> >
> > if(std::is_same_v<decltype(f1), decltype(f2)>)
> > cout << " f1(1) must equal f2(1) is" << boolalpha<< (f1(1)==f2(1));
> > }
> >
> > Try this snippet, and reason about it. That's all what i can say.
>
> But `foo<5>` and `foo<-3>` do not "have the same mapping" of `int` to
> `int`. As such, there is no reason to expect `f1(1)` and `f2(1)` to be
> equal.
>
> Two instantiations of the same template with different parameters
> generate different things. That's *how templates work*.`foo<5>` and
> `foo<-3>` are expected to be different functions with different
> behavior. Two different functions that have the same type are still
> *two different functions*.
> ----
> Hello Jason,
> I guess you get exactly what i mean, you just want to argue.
>
> You say f1 has the same type as f2 , right?
> But at the same time they give me two different values.
> It is like you tell me
> int(1) != int(1) // same type different values.

Types and values aren't the same thing, and equivalent types do not
guarantee equivalent values.

Two objects of type `int` do not need to have the same value just
because they have the same type. Two functions with the signature
`int(int)` do not need to return the same value if you pass them the
same input. This is basic stuff here.

Received on 2024-07-31 23:01:56