0

I'm trying to glean the return type of a class method inside a class that could either be const or non-const. This information is transfered to another calling class object where I try to use decltype() to resolve the return type. However, it doesn't compile with the following errors (among other but I they all boil down to this issue):

Error (gcc 11.2):

<source>:209:11: error: 'decltype' cannot resolve address of overloaded function
  209 |     using proxy_iter_type = decltype(&Container::begin);

My Code:

#include <iostream>
#include <string>
#include <unordered_map>

template<typename Container>
class A_Iterator
{
public:
    using proxy_type = typename Container::proxy_type;
    using proxy_iter_type = decltype(&Container::begin);

public:
    A_Iterator(proxy_iter_type it) : it_{it} {};

private:
    proxy_iter_type it_;
};


class A
{
public:
    using value_type = std::pair<std::string, std::string>;
    using proxy_type = std::unordered_map<int, value_type>;
    using const_iterator = A_Iterator<const A>;
    using iterator = A_Iterator<A>;

public:
    iterator begin() { return iterator( data_.begin() ); }
    const_iterator begin() const { return const_iterator( data_.begin() ); }

private:
    proxy_type data_;
};

How can I get this to work?

glades
  • 3,778
  • 1
  • 12
  • 34
  • Why do you need an iterator that contains the address of the begin function as a member? The code doesn't try to access its return type at all. – n. m. could be an AI Feb 22 '22 at 08:48
  • @n.1.8e9-where's-my-sharem. You can use the function address or just the function name it doesn't matter afaik. How does it not try to access its return type? I need the iterator to contain a member which has the same iterator type as what is being returned by the containers begin function. – glades Feb 22 '22 at 08:55
  • You need `decltype(container.begin())` not `decltype(&Container::begin)`. The latter is utterly useless. `container` should be of type `Container`, i.e. you can use `std::declval()`. – n. m. could be an AI Feb 22 '22 at 08:58
  • You can also use `proxy_type::iterator` instead of all this. – n. m. could be an AI Feb 22 '22 at 08:59
  • BTW you are trying to use the type of `Container::begin`, but thar's just `A_Iterator` itself. You need the iterator of proxy_type, not of the container. – n. m. could be an AI Feb 22 '22 at 09:04
  • @n.1.8e9-where's-my-sharem. Yes you're right, my mistake. In this case I might as well resort to a metafunction to deduce the correct iterator type: `using proxy_iter_type = typename choose::value, typename proxy_type::const_iterator, typename proxy_type::iterator>::type;`. Now it works (after fixing some other bugs)! – glades Feb 22 '22 at 10:38

1 Answers1

0

Try

using proxy_iter_type = decltype(std::declval<const Container>().begin());

or

using proxy_iter_type = decltype(std::declval<Container>().begin());