Opened 10 years ago

Closed 10 years ago

#7775 closed Feature Requests (wontfix)

Allow algorithm::join to accept a container of char*

Reported by: Sergey Mitsyn <svm at jinr.ru> Owned by: Marshall Clow
Milestone: To Be Determined Component: algorithm
Version: Boost 1.52.0 Severity: Problem
Keywords: Cc:

Description

Sometimes it would be nice to join strings in this way:

const char *names[] = {"Jane", "John", "Kate", "Kyle"};

std::cout << boost::algorithm::join(names, ", ") << std::endl;

Can join be fixed to accept char*[]?

I think that one possible change would be in using a wrapper for a range_value<> that would return std::basic_string for a range_value<>::type which is a T* and range_value<>::type if it is something else. Anyway invoking join with a range value of T* with T not representing a character data is erroneous.

Another option is to use the result of evaluation of operator+(range_value, separator) - so a user can pass std::string in the case above to make it valid, but I'm not sure if this is possible and/or portable.

Change History (3)

comment:1 by Marshall Clow, 10 years ago

The problem is not creating from an array of char *, but from a container of char *.

boost::algorithm::join takes a "container (A) of containers (B)" and returns a "container (B)".

When the (B) container is just "char *", it has no facility for creating such a container.

std::string arr[] = { "Hi", "Mom" };
boost::algorithm::join(arr, ", ");

works fine (and returns a std::string)

std::vector<char *> strs;
strs.push_back("Hello");
strs.push_back("World!");
boost::algorithm::join(strs, ", ");

does not.

Last edited 10 years ago by Marshall Clow (previous) (diff)

in reply to:  1 comment:2 by Sergey Mitsyn <svm at jinr.ru>, 10 years ago

Replying to marshall:

The problem is not creating from an array of char *, but from a container of char *.

boost::algorithm::join takes a "container (A) of containers (B)" and returns a "container (B)".

When the (B) container is just "char *", it has no facility for creating such a container.

std::string arr[] = { "Hi", "Mom" };
boost::algorithm::join(arr, ", ");

works fine (and returns a std::string)

Yep, that works perfectly! Didn't know c++03 allows that. Thanks!

comment:3 by Marshall Clow, 10 years ago

Resolution: wontfix
Status: newclosed
Note: See TracTickets for help on using tickets.