See the is >> i
toward the bottom of my code? I want g++ (C++03; I have reasons) to use the first operator>>()
template--the one that prints "non-container type" because the right-hand expression is an int
and not, for example, a vector
. Instead, it considers the last one--the one that prints "fixed-length container type." I can see from the error messages that it is evaluating the last template's enable_if_c<>
conditional argument, which leads to all sorts of problems because has_resize<T>::value
does stuff that won't work on a container.
I would have thought that since the first subexpression in enable_if_c
's conditional parameter, is_container<C>::value
, presumably evaluates to false, the second subexpression, has_resize<C>::value
, would not be evaluated. Either the &&
operator separating the two does not do short-circuiting or the first subexpression inexplicably evaluates to true for an int
. Any idea which one it is and what I can do about it? (Debugging TMP is really difficult. I'd like to step through compilation as the compiler considers each template.)
Oh, and if you change #if 1
to #if 0
, an alternate has_resize<T>
template is used, which works as expected. However, that template doesn't do as good a job determining whether the type is resizable, which is what I'm trying to do. The one I'm trying to get to work doesn't do a perfect job, either, but it's better.
If you'd like to play around with the code, it's also available on Wandbox. (C++ shell, too. I'm playing around with online compilers. I made a list of them.)
#include <iostream>
#include <boost/spirit/home/support/container.hpp>
#if 1
// has_resize<T>::value is whether the (presumably) container class contains resize.
template<class T>
class has_resize
{
struct Fallback { int resize; };
struct Derived : T, Fallback { };
template<class C, C>
class check;
typedef uint8_t no;
typedef uint16_t yes;
template<typename C> static no test(check<int Fallback::*, &C::resize> *);
template<typename C> static yes test(...);
public:
static const bool value = sizeof test<Derived>(0) == sizeof(yes);
};
#else
// has_resize<T>::value is whether the (presumably) container class contains allocator_type.
template <class T>
class has_resize
{
typedef uint8_t yes;
typedef uint16_t no;
template <typename C> static yes test(class C::allocator_type *);
template <typename C> static no test(...);
public:
static const bool value = sizeof test<T>(0) == sizeof(yes);
};
#endif
class xstream { }; // For this example, the class doesn't need to do anything.
template <typename T>
typename boost::enable_if_c<
!boost::spirit::traits::is_container<T>::value,
xstream &>::type
operator>>(xstream &ibs, T &b)
{
std::cout << "non-container type" << std::endl;
return ibs;
}
template <typename C>
typename boost::enable_if_c<
boost::spirit::traits::is_container<C>::value && has_resize<C>::value,
xstream &
>::type
operator>>(xstream &ibs, C &c)
{
std::cout << "variable-length container type" << std::endl;
ibs >> *c.begin();
return ibs;
}
template <typename C>
typename boost::enable_if_c<
boost::spirit::traits::is_container<C>::value && !has_resize<C>::value,
xstream &
>::type
operator>>(xstream &ibs, C &c)
{
std::cout << "fixed-length container type" << std::endl;
ibs >> *c.begin();
return ibs;
}
int main()
{
int i;
xstream is;
is >> i;
}
UPDATE: Here is the code with the fix that @Jarod42 suggested:
#include <iostream>
#include <vector>
#include <set>
#if __cplusplus > 199711L
#include <array>
#endif
#include <boost/spirit/home/support/container.hpp>
#define DEFINE_HAS_SIGNATURE(traitsName, funcName, signature) \
template <typename U> \
class traitsName \
{ \
private: \
template<typename T, T> struct helper; \
template<typename T> \
static char check(helper<signature, &funcName>*); \
template<typename T> static int check(...); \
public: \
static \
const bool value = sizeof(check<U>(0)) == sizeof(char); \
}
#if __cplusplus > 199711L
DEFINE_HAS_SIGNATURE(has_resize, T::resize, void (T::*)(typename T::size_type));
#else
DEFINE_HAS_SIGNATURE(has_resize, T::resize, void (T::*)(typename T::size_type, typename T::value_type));
#endif
class xstream { }; // For this example, the class doesn't need to do anything.
template <typename T>
typename boost::enable_if_c<
!boost::spirit::traits::is_container<T>::value,
xstream &>::type
operator>>(xstream &ibs, T &b)
{
std::cout << "non-container type" << std::endl;
return ibs;
}
template <typename C>
typename boost::enable_if_c<
boost::spirit::traits::is_container<C>::value && has_resize<C>::value,
xstream &
>::type
operator>>(xstream &ibs, C &c)
{
std::cout << "variable-length container type" << std::endl;
ibs >> *c.begin();
return ibs;
}
template <typename C>
typename boost::enable_if_c<
boost::spirit::traits::is_container<C>::value && !has_resize<C>::value,
xstream &
>::type
operator>>(xstream &ibs, C &c)
{
std::cout << "fixed-length container type" << std::endl;
ibs >> *c.begin();
return ibs;
}
int main()
{
int i;
std::vector<int> vi;
std::set<int> si;
#if __cplusplus > 199711L
std::array<int, 1> ai;
#endif
xstream xs;
xs >> i >> vi >> si;
#if __cplusplus > 199711L
xs >> ai;
#endif
}
In your case, you have a hard error in
struct Derived : T, Fallback { };
withT = int
The::value
force the instantiation of the class.I use the following:
Live Demo