C++ Logo

std-proposals

Advanced search

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

From: Tiago Freire <tmiguelf_at_[hidden]>
Date: Fri, 2 Aug 2024 16:48:26 +0000
By using https://en.cppreference.com/w/cpp/utility/any/type

It would have been very weird if we had a type that hide your data and then couldn't recover it, that would be the sort of thing that shouldn't end up in the standard. Good thing that is not what happened.
________________________________
From: Std-Proposals <std-proposals-bounces_at_[hidden]> on behalf of organicoman via Std-Proposals <std-proposals_at_[hidden]>
Sent: Friday, August 2, 2024 6:35:21 PM
To: Marcin Jaczewski <marcinjaczewski86_at_[hidden]>
Cc: organicoman <organicoman_at_[hidden]>; std-proposals_at_[hidden] <std-proposals_at_[hidden]>
Subject: Re: [std-proposals] Function overload set type information loss



Lest be charitable and assume you mean this code:


```
template<typename T>
void foo() { cout <<typeid(T).name()<<endl;}

auto useType(effdecltype(foo) f)
{
  using T = __get_ortho_at__<0>(f);
  f();
  return T(0);
}

template<typename T>
void consumeType(T t)
{ cout << typeid(t).name(); }

int main()
{
    vector<effdecltype(foo)> vec;
    vec.push_back(&foo<int>);
    vec.push_back(&foo<double>);
    vec.push_back(&foo<float>);
    for(int i=0; i<5; ++i)
        vec.push_back(&foo<char>);
    for(const auto& F: vec)
       consumeType(useType(F));
}
```


It can be easy fixed by helper:


```
template<typename T>
void consumeTypeHelper()
{
    foo<T>();
    return consumeType(T{});
}

int main()
{
    vector<void(*)()> vec;
    vec.push_back(&consumeTypeHelper<int>);
    vec.push_back(&consumeTypeHelper<double>);
    vec.push_back(&consumeTypeHelper<float>);
    for(int i=0; i<5; ++i)
        vec.push_back(&consumeTypeHelper<char>);
    for(const auto& F: vec)
       F();
}
```

Done. Problem fixed in C++11

Very good... nice try.
Unfortunately you miss the point.

I showed a loop over a set of functions returning many types.
Imagine you have a container of slots, which returns different types ( i already said that many times)
Given the current state of C++ you can store them only as. Let say.
vector<std::any(*)(int, double)>
How can you tell what type the called function returned before it was hiden inside std::any?
Is it clear?



Received on 2024-08-02 16:48:30