简体   繁体   中英

What is the purpose of a `std::variant` with a single template parameter?

In the example on this page: https://en.cppreference.com/w/cpp/utility/variant there is an std::variant with a single template parameter:

std::variant<std::string> x("abc");

Since a variant allows to store an object which can belong to a set of different classes, what is the purpose of the previous line? Should not it be the same as:

std::string x("abc");

Its purpose is to demonstrate that std::variant can use one of the variant type's converting constructors as long as it is unambiguous.

The example probably could have been a little better by using

std::variant<std::string, int> x("abc");

instead, to show that off more clearly.

Depends what you mean by "the same". Those two lines of code definitely don't have the same layout in memory, nor the same access pattern.

As to the reason for that to exist, well -- symmetry. If you're going to support a large number of template arguments, nothing's stopping you from also supporting one. In fact it can make the implementation easier if it went the recursive route (like C#'s value tuples). So just throw it in and let people use it if they want.

The point of that particular line is to demonstrate "converting constructors work when unambiguous".

The point of allowing a std::variant with a single template parameter is that there would be an implementation cost and insufficient benefit to justify the cost.

One benefit of allowing a std::variant with a single template parameter comes from the power of templates. Perhaps a library provides a template for which one of the parameters must be a std::variant . Conceptually, this would be a parameter enumerating the various types that some aspect of the template must handle. (Whether or not this is a good approach is a separate question. It's also an academic question for users of the library.) Suppose you have a use for this class, but you need it to handle only a single type. No problem. You use std::variant<the_type> as the template argument. A special case was handled for free (if the template is bug-free), even if the library's authors did not anticipate this use-case. The template is more versatile, covering more cases, than if a std::variant had to have at least two template arguments.

Is this too theoretical / unlikely to happen in practice? I don't have an example for a variant , but I do have one for a tuple . Similar to this question, one could ask about the purpose of a tuple with a single template parameter. The context of Q: C++ tbb flow graph, multifunction_node giving incomplete type error involves a library template that expects one of its parameters to be a tuple of types. The author of that question had only a single type. Providing a tuple of that one type allowed the template to be used. That library template is more versatile than it would be if tuple had to take at least two template arguments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM