Boost logo

Boost :

From: Atry (pop.atry_at_[hidden])
Date: 2007-08-10 06:08:39


Hi, I am a user of Boost.Range. I have a problem when I write a function.
The function accept a char range, wchar_t range or it, and each have
different implement, so I want to use enable_if:

///
#include <boost/range.hpp>
#include <boost/utility/enable_if.hpp>
#include <boost/type_traits/is_same.hpp>
using namespace boost;
template<typename CharRange>
typename enable_if<is_same<typename range_value<CharRange>::type, char>
>::type my_range_function(CharRange const &) {
  // for char
}

template<typename CharRange>
typename enable_if<is_same<typename range_value<CharRange>::type, wchar_t>
>::type my_range_function(CharRange const &) {
  // for wchar_t
}

void my_range_function(int) {
  // for int
}
int main() {
  my_range_function("char range");
  my_range_function(L"wchar_t range");
  my_range_function(1);
}
///

The code does not work because range_value<CharRange>::type is not lazy, so
I hope there is an metafunction "is_range_of" could be used like this:

///
#include <boost/range.hpp>
#include <boost/utility/enable_if.hpp>
#include <boost/type_traits/is_same.hpp>
using namespace boost;
template<typename CharRange>
typename enable_if<is_range_of<char, CharRange> >::type
my_range_function(CharRange const &) {
  // for char
}

template<typename CharRange>
typename enable_if<is_range_of<wchar_t, CharRange> >::type
my_range_function(CharRange const &) {
  // for wchar_t
}

void my_range_function(int) {
  // for int
}
int main() {
  my_range_function("char range");
  my_range_function(L"wchar_t range");
  my_range_function(1);
}
///

However, I upload a implement for the feature.





Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk