Kobi
Kobi

Reputation: 878

Template keyword as template argument

Reading functional programming in c++ by Ivan Čukić seeing this towards the end of section 9.1.4.

What is this syntax "template Variant" and "template Expected" coming from?

template <typename T, template Variant,
      template Expected = expected<T, std::string>>
Expected get_if(const Variant& variant)
{
    T* ptr = std::get_if<T>(variant);
    if (ptr) {
       return Expected::success(*ptr);
    } else {
       return Expected::error("Variant doesn't contain the desired type");
    }
}

Is this valid C++ syntax? It does not look like template template parameter.

Trying a small toy sample on godbolt does not work for me.

template <typename T, template Variant, template E = std::map<T, std::string>>
int f(const Variant& v) {
   return std::get<0>(v);
}
int main() {
   std::variant<int> v{0};
   return f(v);
}

Upvotes: 0

Views: 51

Answers (1)

Chris Uzdavinis
Chris Uzdavinis

Reputation: 6131

It's a typo in the book. Replace those two appearances of "template" with "typename".

Here's the link for errata. https://forums.manning.com/posts/list/45184.page

Upvotes: 2

Related Questions