0

I am trying to understand boost recursive variant and template. For the below code, I get compilation error of

cannot convert argument 1 from 'std::vector<std::string,std::allocator<_Ty>>' to 'const Boost::variant<boost::detail::variant::recursive_flag<T0>,T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13,T14,T15,T16,T17,T18,T19> &'

Error seems to be originating from GetVarValue() function

As per my undertanding I can have a class with _default : which can be vector of int, string, bool, double, vector of int/string/bool/double right?

    typedef boost::make_recursive_variant<
        int,
        std::string,
        bool,
        double,
        std::vector<boost::recursive_variant_> >::type TokenValueType;


    template<class T>
    class VectorToken : public Token
    {
    private:

        string _name;

        vector<T> _default;

    public:
        VectorToken(string name, vector<T> defaultVal)
            : _name(name)
        {
            _default = defaultVal;
        }

        virtual TokenValueType GetVarValue()
        {
            return _default;  <-- _default (vector<string>) doesn't like TokenValueType ?
        }
    };

    main()
    {
        vector<string> tempvec = {"ABC", "LMN", "PQR"};
        VectorToken<string> t4 = VectorToken<string>::VectorToken("VECTORSTR", tempvec);
    }

The error goes away when I write main like this:

main()
{
    vector<TokenValueType> tempvec = { string("ABC"), string("LMN"), string("PQR") };
    VectorToken<TokenValueType> t4 = VectorToken<TokenValueType>::VectorToken("VECTORSTR", tempvec);

}
legameeternoforall
  • 69
  • 1
  • 2
  • 10

1 Answers1

0

I will simplify that type:

typedef boost::make_recursive_variant<
    int,
    std::string,
    std::vector<boost::recursive_variant_> >::type TokenValueType;

This should be enough to explain it.

TokenValueType is not a variant over (vector of int, vector of string, vector of vector of TokenValueType).

It is a vector over (int or string or TokenValueType).

Here is a MCVE with your syntax errors corrected and one line changed:

return std::vector<TokenValueType>(_default.begin(), _default.end());
Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524