If I have a class with std::atomic_bool or std::mutex member for example, and if I put this class inside std::variant, my g++ will complain with "no matching function for call to std::variant<....>". Now I have to declare my std::mutex member to be static.

g++ (GCC) 7.3.1 20180303 (Red Hat 7.3.1-5) Copyright (C) 2017 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Actual code

#include <iostream>
#include <variant>
#include <mutex>

enum class enFixEngineRunMode {
    eFixModeStreet      // Fix connection side as initiator/client
    ,eFixModeStreetStandAlone       // Fix connection side as initiator/client
    ,eFixModeStreetAccpt    // Fix connection side as acceptor/server
    ,eFixModeStreetAccptStandAlone  // Fix connection side as acceptor/server
    ,eFixModeClient     // Fix connection side as acceptor/client
    ,eFixModeClientStandAlone       // Fix connection side as acceptor/client
    ,eFixModeClientInit // Fix connection side as initiator/server
    ,eFixModeClientInitStandAlone   // Fix connection side as initiator/server
    ,eFixModeInvalid
};

struct FOO {
    FOO(int any) { }
    void operator()() const {
        std::cout << "FOO2" << std::endl;
    }
};

template <enum enFixEngineRunMode>
struct BAR {
    BAR(double any) { }
    void operator()() const {
        std::cout << "BAR2" << std::endl;
    }

    std::mutex  m_metux;
};

template<>
struct BAR<enFixEngineRunMode::eFixModeStreetStandAlone> {
    BAR(double any) { }
    void operator()() const {
        std::cout << "eFixModeStreetStandAlone" << std::endl;
    }
};

using EngineImpl = std::variant<BAR<enFixEngineRunMode::eFixModeStreet>
                                , BAR<enFixEngineRunMode::eFixModeStreetStandAlone>
                                , BAR<enFixEngineRunMode::eFixModeStreetAccpt>
                                , BAR<enFixEngineRunMode::eFixModeStreetAccptStandAlone>
                                , BAR<enFixEngineRunMode::eFixModeClient>
                                , BAR<enFixEngineRunMode::eFixModeClientStandAlone>
                                , BAR<enFixEngineRunMode::eFixModeClientInit>
                                , BAR<enFixEngineRunMode::eFixModeClientInitStandAlone>
                                , BAR<enFixEngineRunMode::eFixModeInvalid>>;

struct Engine {
    Engine() : m_engine([&] {
        int i = 2;
        if (1 == i)
            return EngineImpl(BAR<enFixEngineRunMode::eFixModeStreetStandAlone>(0.0));
        else return EngineImpl(BAR<enFixEngineRunMode::eFixModeStreet>(0.0));
    }()) {}

    void operator()() const {
        std::visit([](auto const& e){ e(); }, m_engine);
    }
    EngineImpl  m_engine;
};

int main(int argc, const char *argv[], char** env)
{
    Engine e;
    e();
    return 0;
}

compilation error:

variantMain2.cpp:57:70: error: no matching function for call to ‘std::variant<BAR<(enFixEngineRunMode)0>, BAR<(enFixEngineRunMode)1>, BAR<(enFixEngineRunMode)2>, BAR<(enFixEngineRunMode)3>, BAR<(enFixEngineRunMode)4>, BAR<(enFixEngineRunMode)5>, BAR<(enFixEngineRunMode)6>, BAR<(enFixEngineRunMode)7>, BAR<(enFixEngineRunMode)8> >::variant(BAR<(enFixEngineRunMode)0>)’
   else return EngineImpl(BAR<enFixEngineRunMode::eFixModeStreet>(0.0));
                                                                      ^
In file included from variantMain2.cpp:2:0:
/opt/rh/devtoolset-7/root/usr/include/c++/7/variant:986:2: note: candidate: template<long unsigned int _Np, class _Up, class ... _Args, class> constexpr std::variant<_Types>::variant(std::in_place_index_t<_Np>, std::initializer_list<_Up>, _Args&& ...)
  variant(in_place_index_t<_Np>, initializer_list<_Up> __il,
  ^~~~~~~
/opt/rh/devtoolset-7/root/usr/include/c++/7/variant:986:2: note:   template argument deduction/substitution failed:
variantMain2.cpp:57:70: note:   ‘BAR<(enFixEngineRunMode)0>’ is not derived from ‘std::in_place_index_t<_Idx>’
   else return EngineImpl(BAR<enFixEngineRunMode::eFixModeStreet>(0.0));
                                                                      ^
In file included from variantMain2.cpp:2:0:
/opt/rh/devtoolset-7/root/usr/include/c++/7/variant:977:2: note: candidate: template<long unsigned int _Np, class ... _Args, class> constexpr std::variant<_Types>::variant(std::in_place_index_t<_Np>, _Args&& ...)
  variant(in_place_index_t<_Np>, _Args&&... __args)

1

There are 1 answers

1
Davis Herring On BEST ANSWER

Because std::variant is not an aggregate, it must move its arguments into its internal storage, and std::mutex is not movable (because doing so would break any concurrent users). Your choices are to make BAR movable (e.g., by storing a std::unique_ptr<std::mutex>), or to avoid the move by using std::in_place_type to construct the object inside the variant.