Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++17 variant<any> inside the class

The following code compiles well:

int main()
{
    variant<any> var;
    var = 5;
    cout << any_cast<int>(get<any>(var)) << endl;
    return 0;
}

But when I'm trying to put variant<any> as a class member

struct MyClass{
    variant<any> var;
};

int main()
{
    MyClass s;
    return 0;
}

It doesn't compile. Am I doing something wrong or it is some bug?

I'm using gcc 7.1.0

In file included from /home/zak/Projects/Anytest/main.cpp:3:0:
/usr/local/gcc-7.1/include/c++/7.1.0/variant: In instantiation of ‘struct std::__detail::__variant::__accepted_index<const std::variant<std::any>&, std::variant<std::any>, void>’:
/usr/local/gcc-7.1/include/c++/7.1.0/variant:911:26:   required from ‘constexpr const size_t std::variant<std::any>::__accepted_index<const std::variant<std::any>&>’
/usr/local/gcc-7.1/include/c++/7.1.0/variant:940:6:   required by substitution of ‘template<class _Tp, class> constexpr std::variant<std::any>::variant(_Tp&&) [with _Tp = const std::variant<std::any>&; <template-parameter-1-2> = <missing>]’
/home/zak/Projects/Anytest/main.cpp:14:13:   required from here
/usr/local/gcc-7.1/include/c++/7.1.0/variant:559:49: error: no matching function for call to ‘std::__detail::__variant::__overload_set<std::any>::_S_fun(const std::variant<std::any>&)’
       decltype(__overload_set<_Types...>::_S_fun(std::declval<_Tp>()),
                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
/usr/local/gcc-7.1/include/c++/7.1.0/variant:541:58: note: candidate: static std::integral_constant<long unsigned int, sizeof... (_Rest)> std::__detail::__variant::__overload_set<_First, _Rest ...>::_S_fun(_First) [with _First = std::any; _Rest = {}]
       static integral_constant<size_t, sizeof...(_Rest)> _S_fun(_First);
                                                          ^~~~~~
/usr/local/gcc-7.1/include/c++/7.1.0/variant:541:58: note:   no known conversion for argument 1 from ‘const std::variant<std::any>’ to ‘std::any’
/usr/local/gcc-7.1/include/c++/7.1.0/variant:535:19: note: candidate: static void std::__detail::__variant::__overload_set<_Types>::_S_fun() [with _Types = {}]
     { static void _S_fun(); };
                   ^~~~~~
/usr/local/gcc-7.1/include/c++/7.1.0/variant:535:19: note:   candidate expects 0 arguments, 1 provided
/usr/local/gcc-7.1/include/c++/7.1.0/variant:559:49: error: no matching function for call to ‘std::__detail::__variant::__overload_set<std::any>::_S_fun(const std::variant<std::any>&)’
       decltype(__overload_set<_Types...>::_S_fun(std::declval<_Tp>()),
                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
/usr/local/gcc-7.1/include/c++/7.1.0/variant:541:58: note: candidate: static std::integral_constant<long unsigned int, sizeof... (_Rest)> std::__detail::__variant::__overload_set<_First, _Rest ...>::_S_fun(_First) [with _First = std::any; _Rest = {}]
       static integral_constant<size_t, sizeof...(_Rest)> _S_fun(_First);
                                                          ^~~~~~
/usr/local/gcc-7.1/include/c++/7.1.0/variant:541:58: note:   no known conversion for argument 1 from ‘const std::variant<std::any>’ to ‘std::any’
/usr/local/gcc-7.1/include/c++/7.1.0/variant:535:19: note: candidate: static void std::__detail::__variant::__overload_set<_Types>::_S_fun() [with _Types = {}]
     { static void _S_fun(); };
                   ^~~~~~
/usr/local/gcc-7.1/include/c++/7.1.0/variant:535:19: note:   candidate expects 0 arguments, 1 provided
/usr/local/gcc-7.1/include/c++/7.1.0/variant:559:49: error: no matching function for call to ‘std::__detail::__variant::__overload_set<std::any>::_S_fun(const std::variant<std::any>&)’
       decltype(__overload_set<_Types...>::_S_fun(std::declval<_Tp>()),
                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
/usr/local/gcc-7.1/include/c++/7.1.0/variant:541:58: note: candidate: static std::integral_constant<long unsigned int, sizeof... (_Rest)> std::__detail::__variant::__overload_set<_First, _Rest ...>::_S_fun(_First) [with _First = std::any; _Rest = {}]
       static integral_constant<size_t, sizeof...(_Rest)> _S_fun(_First);
                                                          ^~~~~~
/usr/local/gcc-7.1/include/c++/7.1.0/variant:541:58: note:   no known conversion for argument 1 from ‘const std::variant<std::any>’ to ‘std::any’
/usr/local/gcc-7.1/include/c++/7.1.0/variant:535:19: note: candidate: static void std::__detail::__variant::__overload_set<_Types>::_S_fun() [with _Types = {}]
     { static void _S_fun(); };
                   ^~~~~~
/usr/local/gcc-7.1/include/c++/7.1.0/variant:535:19: note:   candidate expects 0 arguments, 1 provided
/usr/local/gcc-7.1/include/c++/7.1.0/variant: In instantiation of ‘constexpr const size_t std::__detail::__variant::__accepted_index<const std::variant<std::any>&, std::variant<std::any>, void>::value’:
/usr/local/gcc-7.1/include/c++/7.1.0/variant:911:26:   required from ‘constexpr const size_t std::variant<std::any>::__accepted_index<const std::variant<std::any>&>’
/usr/local/gcc-7.1/include/c++/7.1.0/variant:940:6:   required by substitution of ‘template<class _Tp, class> constexpr std::variant<std::any>::variant(_Tp&&) [with _Tp = const std::variant<std::any>&; <template-parameter-1-2> = <missing>]’
/home/zak/Projects/Anytest/main.cpp:14:13:   required from here
/usr/local/gcc-7.1/include/c++/7.1.0/variant:564:12: error: no matching function for call to ‘std::__detail::__variant::__overload_set<std::any>::_S_fun(const std::variant<std::any>&)’
  - decltype(__overload_set<_Types...>::
             ~~~~~~~~~~~~~~~~~~~~~~~~~~~
      _S_fun(std::declval<_Tp>()))::value;
      ~~~~~~^~~~~~~~~~~~~~~~~~~~~
/usr/local/gcc-7.1/include/c++/7.1.0/variant:541:58: note: candidate: static std::integral_constant<long unsigned int, sizeof... (_Rest)> std::__detail::__variant::__overload_set<_First, _Rest ...>::_S_fun(_First) [with _First = std::any; _Rest = {}]
       static integral_constant<size_t, sizeof...(_Rest)> _S_fun(_First);
                                                          ^~~~~~
/usr/local/gcc-7.1/include/c++/7.1.0/variant:541:58: note:   no known conversion for argument 1 from ‘const std::variant<std::any>’ to ‘std::any’
/usr/local/gcc-7.1/include/c++/7.1.0/variant:535:19: note: candidate: static void std::__detail::__variant::__overload_set<_Types>::_S_fun() [with _Types = {}]
     { static void _S_fun(); };
                   ^~~~~~
/usr/local/gcc-7.1/include/c++/7.1.0/variant:535:19: note:   candidate expects 0 arguments, 1 provided
like image 377
Zak Avatar asked May 13 '17 10:05

Zak


1 Answers

The problem is with MyClass's implicitly defined copy constructor, when it tries to copy construct the member of type std::variant<std::any>.

To perform overload resolution, the compiler first needs to try to instantiate all of std::variant's constructor templates, with the function argument being a const std::variant<std::any>&1. The constructor of our interest is this one:

template <class T> constexpr variant(T&& t) noexcept(/*...*/);

It only participates in overload resolution if, among others, the expression FUN(​std​::​forward<T>(t)) is well formed, where FUN is a set of overloaded functions produced according to [variant.ctor]/12.2

In this case, there is only one alternative type (std::any), so there is only one imaginary function FUN, whose signature is FUN(std::any). Now, the compiler needs to decide whether FUN can be called with a const std::variant<std::any>&1. In this process, the compiler needs to know whether std::any can be constructed with const std::variant<std::any>&1.

This will trigger the instantiation of std::any's constructor template template<class T> any(T&& value);, which only participates in overload resolution if std::is_­copy_­constructible_­v<VT> is true (VT being std::decay_t<T>, and T being const std::variant<std::any>&).

Now in order to see whether VT (i.e. std::variant<std::any>) is copy constructible, the compiler needs to try to instantiate all of std::variant's constructor templates... and this is where we started, and we are stuck in a loop.

This can explain why we see template<class _Tp, class> constexpr std::variant<std::any>::variant(_Tp&&) and __overload_set<std::any>::_S_fun (which corresponds to the function FUN mentioned above) in the error message, and why we see the same error appearing several times.

It remains a question how GCC breaks from the above loop, and why tweaking the program can stop GCC from reporting the error. Maybe these are indication of some bug.


1. Strictly speaking, it should be "an lvalue of type const std::variant<std::any>" rather than "a const std::variant<std::any>&".

2. The standard also requires that this constructor shall only participate in overload resolution if is_­same_­v<decay_­t<T>, variant> is false. GCC (libstdc++) chooses to check that later. I don't know whether this is conforming.

like image 185
cpplearner Avatar answered Nov 15 '22 16:11

cpplearner