2

Here's a small example which is substantially similar to what I'm trying to do:

#include <boost/variant/variant.hpp>
#include <boost/variant/recursive_wrapper.hpp>
#include <utility>
#include <vector>

struct foo {
    const char * str;
};

typedef std::pair<float, float> fpair;

//typedef std::vector<boost::variant<int, fpair, foo, vlist>> vlist;
// ^ No...

//typedef std::vector<boost::variant<int, fpair, foo, boost::recursive_wrapper<vlist>>> vlist;
// ^ No...

//template <typename T = vlist<T> >
//using vlist = std::vector<boost::variant<int, fpair, foo, boost::recursive_wrapper<vlist>>>;
// ^ No...

template <typename T = vlist<T> >
using vlist = std::vector<boost::variant<int, fpair, foo, boost::recursive_wrapper<T>>>;
// Still no?

int main () {
    std::cout << "Hello world\n";
}

The error I get with gcc 4.8 is:

test.cpp:12:33: error: expected nested-name-specifier before ‘vlist’
template <typename T = typename vlist<T>>
                                ^
test.cpp:12:33: error: expected ‘>’ before ‘vlist’

The error with clang 3.6 is:

test.cpp:12:24: error: unknown type name 'vlist'
template <typename T = vlist<T>>
                       ^
test.cpp:12:29: error: expected ',' or '>' in template-parameter-list
template <typename T = vlist<T>>
                            ^
test.cpp:12:32: error: expected unqualified-id
template <typename T = vlist<T>>
                               ^
3 errors generated.

(Edit: actually these errors are from slightly different versions of the above code, but they all give quite similar messages)

I looked at these earlier, slightly different questions, I'm still stumped:

How to declare a self referencing template type

How to properly declare a self-referencing template type?

Boost Fusion adapt declaration for a templated self referential structure

Does anyone know a trick for this, or is there some reason I'm not aware of that the compiler inherently isn't able to do this?

Community
  • 1
  • 1
Chris Beck
  • 15,614
  • 4
  • 51
  • 87

1 Answers1

2

I believe you just want boost::make_recursive_variant:

#include <boost/variant/variant.hpp>
#include <boost/variant/recursive_variant.hpp>
#include <utility>
#include <vector>

struct foo {
    const char* str;
};

typedef std::pair<float, float> fpair;

typedef boost::make_recursive_variant<
    int,
    fpair,
    foo,
    std::vector<boost::recursive_variant_>
>::type vlist;

int main() {
    std::vector<vlist> vec;
    vec.push_back(4);
    vec.push_back(fpair{1.0f, 2.0f});

    vlist v2(vec);
}
Barry
  • 286,269
  • 29
  • 621
  • 977