2

When i'm trying to use emplace method of unordered_map my IDE (CLion 2021.1.2 Build #CL-211.7442.42) inserts a template keyword before emplace method. I don't get it why it is used in my case? Example:

template<typename T>
class Store {
public:
  void Set(std::string const& key, T value) {
    storage_.template emplace(key, std::move(value));
  }
private:
  std::unordered_map<std::string, T> storage_;
};
senloa
  • 181
  • 1
  • 8
  • 1
    Not sure which IDE you're using... but `template` shouldn't be there. – Mgetz Sep 24 '21 at 15:58
  • @mgetz CLIon (intellij idea) – senloa Sep 24 '21 at 15:59
  • 1
    Something strange - compiles with clang and gcc, but not with MSVC - live - https://godbolt.org/z/EoWTMecKs Perhaps add [language-lawyer] as we have compiler differences ? – Richard Critten Sep 24 '21 at 16:04
  • If I had to guess it's because `std::unordered_set::emplace` is itself a templated member function https://en.cppreference.com/w/cpp/container/unordered_set/emplace so it could be a dependant name. – Richard Critten Sep 24 '21 at 16:08
  • @RichardCritten AFAIK that would trigger `typename`? I've never seen `template` used that way, or even heard of needing it. – Mgetz Sep 24 '21 at 16:10
  • @Mgetz _"...Inside a declaration or a definition of a template, typename can be used to declare that a dependent qualified name is a type...."_ - https://en.cppreference.com/w/cpp/keyword/typename Still it's not a type so I am still with "something strange" – Richard Critten Sep 24 '21 at 16:12
  • @RichardCritten I'm well aware of `typename` I mean using `template` like that. – Mgetz Sep 24 '21 at 16:12
  • @Mgetz oops my mistake sorry. – Richard Critten Sep 24 '21 at 16:13
  • 2
    @Mgetz how about _"...Inside a template definition, template can be used to declare that a dependent name is a template..."_ https://en.cppreference.com/w/cpp/keyword/template and this matches because `std::unordered_set::emplace` is a member template. – Richard Critten Sep 24 '21 at 16:14
  • so based on the linked question it seems this use of the `template` keyword is obsolete largely. – Mgetz Sep 24 '21 at 16:20

0 Answers0