[libcxx-commits] [PATCH] D97394: [libc++] [C++2b] [P2162] Allow inheritance from std::variant.
Tim Song via Phabricator via libcxx-commits
libcxx-commits at lists.llvm.org
Thu Mar 4 12:54:00 PST 2021
tcanens added inline comments.
================
Comment at: libcxx/test/std/utilities/variant/variant.visit/visit.pass.cpp:376
+
+ struct EvilVariant2 : std::variant<int, long>, std::type_info {};
+
----------------
curdeius wrote:
> curdeius wrote:
> > Quuxplusone wrote:
> > > Would it be appropriate to also test the behavior of `struct X : std::variant<int>, std::variant<long> {}` and `struct Y : private std::variant<int> {}`? I don't think we expect either of those to support `std::visit`, but I wonder if they're supposed to SFINAE away cleanly or give some awful template error message or what.
> > I don't see how it may be SFINAE-friendly (but I haven't checked).
> Well, I've checked those and they fail with more or less cryptic messages, but still I find them pretty nice.
> E.g.
> ```
> struct BadVariant : std::variant<int>, std::variant<double> {
> using std::variant<int>::variant;
> };
> std::visit([](auto x) { assert(x == 12); }, BadVariant{12});
> ```
> fails with:
> ```
> /home/marek/llvm-project/libcxx/include/variant:1669:36: error: no matching function for call to '__as_variant'
> const bool __valueless = (... || _VSTD::__as_variant(__vs).valueless_by_exception());
> ^~~~~~~~~~~~~~~~~~~
> /home/marek/llvm-project/libcxx/include/__config:779:15: note: expanded from macro '_VSTD'
> #define _VSTD std::_LIBCPP_ABI_NAMESPACE
> ^
> /home/marek/llvm-project/libcxx/include/variant:1680:10: note: in instantiation of function template specialization 'std::__throw_if_valueless<BadVariant>' requested here
> _VSTD::__throw_if_valueless(_VSTD::forward<_Vs>(__vs)...);
> ^
> /home/marek/llvm-project/libcxx/test/std/utilities/variant/variant.visit/visit.pass.cpp:397:8: note: in instantiation of function template specialization 'std::visit<(lambda at /home/marek/llvm-project/libcxx/test/std/utilities/variant/variant.visit/visit.pass.cpp:397:14), BadVariant>' requested here
> std::visit([](auto x) { assert(x == 12); }, BadVariant{12});
> ^
> /home/marek/llvm-project/libcxx/include/variant:326:1: note: candidate template ignored: failed template argument deduction
> __as_variant(variant<_Types...>& __vs) noexcept {
> ^
> /home/marek/llvm-project/libcxx/include/variant:332:1: note: candidate template ignored: failed template argument deduction
> __as_variant(const variant<_Types...>& __vs) noexcept {
> ^
> ...
> ```
>
> I think there's nothing we can do about it.
The wording is pretty clear that this case needs to SFINAE.
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D97394/new/
https://reviews.llvm.org/D97394
More information about the libcxx-commits
mailing list