template function question - using value_type

T

titanandrews

Hi All,

I'm trying to create a helper function that converts a collection of T
into a vector of std::string.
The toStdStringFunc is so the caller can provide the T to std::string
conversion.
I want to use Collection::value_type as the first param of the
toStdStringFunc, but this does not compile.
Is there any way to make this work using templates?


template<class Collection>
static void fromCollectionT(const Collection& collection,
std::vector<Location_t>& locations, boost::function<void (const
Collection::value_type&, std::string& )> toStdStringFunc)
{

}

The following does work, but it's a little verbose to the caller and I
would like to avoid the extra class ValueType if possible. Any way to
do it?

template<class Collection, class ValueType>
static void fromCollectionT(const Collection& collection,
std::vector<Location_t>& locations, boost::function<void (const
ValueType&, std::string& )> toStdStringFunc)
{

}

many thanks,

B
 
L

Luc Danton

Hi All,

I'm trying to create a helper function that converts a collection of T
into a vector of std::string.
The toStdStringFunc is so the caller can provide the T to std::string
conversion.
I want to use Collection::value_type as the first param of the
toStdStringFunc, but this does not compile.
Is there any way to make this work using templates?


template<class Collection>
static void fromCollectionT(const Collection& collection,
std::vector<Location_t>& locations, boost::function<void (const
Collection::value_type&, std::string& )> toStdStringFunc)
{

}

The following does work, but it's a little verbose to the caller and I
would like to avoid the extra class ValueType if possible. Any way to
do it?

template<class Collection, class ValueType>
static void fromCollectionT(const Collection& collection,
std::vector<Location_t>& locations, boost::function<void (const
ValueType&, std::string& )> toStdStringFunc)
{

}

many thanks,

B

Perhaps the compiler message could be of help as to why this "doesn't
compile"? As things are, I think you're missing a typename, as in

template<class Collection>
static void
fromCollectionT(
const Collection& collection,
std::vector<Location_t>& locations,
boost::function<
void(_typename_ const Collection::value_type&, std::string&)
 
T

titanandrews

Perhaps the compiler message could be of help as to why this "doesn't
compile"? As things are, I think you're missing a typename, as in

template<class Collection>
static void
fromCollectionT(
   const Collection& collection,
   std::vector<Location_t>&  locations,
   boost::function<
     void(_typename_ const Collection::value_type&, std::string&)
   > toStdStringFunc);

Good grief! That was it. The compiler did tell me that actually. I
just lost the message in some other errors. Thanks for your response!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,585
Members
45,082
Latest member
KetonaraKetoACV

Latest Threads

Top