Gustavo N
Gustavo N

Reputation: 23

Compilation error with Type Traits in boost::type_traits::conditional

I am having a problem in some code using type_traits from boost. It is quite a complex part of the code, but I could isolate the part that gives the compilation error:

template<const size_t maxLen>
class MyString {
public:
    typedef boost::conditional<(maxLen > 0), char[maxLen+1], std::string> ObjExternal;
};

template <class T>
class APIBase {
public:
    typedef T obj_type;
    typedef typename T::ObjExternal return_type;
};

template <class T>
int edit(const T& field, const typename T::return_type& value)
{
    return 0;
}

int myFunction()
{
    APIBase<MyString<10> > b;
    char c[11];
    return edit(b, c);
}

This gives the following error:

test.cpp: In function ‘int myFunction()’: tes.cpp:109: error: no matching function for call to ‘edit(APIBase >&, char [11])’ tes.cpp:100: note: candidates are: int edit(const T&, const typename T::return_type&) [with T = APIBase >]

However, if I change the line with the code

char c[11];

by

MyString<10>::ObjExternal c;

it works. Similarly, if instead I change the line

typedef boost::conditional<(maxLen > 0), char[maxLen+1], std::string> ObjExternal;

by

typedef char ObjExternal[maxLen+1];

it also works. I am thinking that it is a problem with boost::conditional, as it seems it is not being evaluated right. Is there a problem in my code, or there is an alternative that can be used instead of boost::conditional to have this functionality?

I am thinking about using the 2nd option, but then I could not use maxLen as 0.

Upvotes: 0

Views: 1062

Answers (1)

Tanner Sansbury
Tanner Sansbury

Reputation: 51941

You need to use the member typedef type provided by conditional and not the conditional type itself.

Change:

typedef boost::conditional<(maxLen > 0),
                           char[maxLen+1],
                           std::string> ObjExternal;

to:

typedef typename boost::conditional<(maxLen > 0),
                                    char[maxLen+1],
                                    std::string>::type ObjExternal;

Upvotes: 1

Related Questions