Home > Enterprise >  How to deduce template type based on another template type
How to deduce template type based on another template type

Time:01-26

I have a templated class, whose type is to be determined by another sub templated constructor.

template <typename V, typename I>
class Text{
  public:

    template <typename Container, typename V = typename Container::value_type, typename I = typename Container::size_type>
    Text(Container& c) {}

};

So usage would be something like:

std::vector<int> v;
Text(v) //Deduces to Text<int, std::size_t>

Unfortunately, I get an error that V and I are being shadowed. This is because it is trying to create a new V and I.

I have seen a solution with using V = typename Container::value_type; but that gives the same error.

CodePudding user response:

Your probably want to write your own deduction guide:

template <typename Container>
Text(Container&) -> Text<typename Container::value_type,
                         typename Container::size_type>;

Demo

  •  Tags:  
  • Related