Trying to convert a vector of std::string to a vector of const char*:
#include <algorithm>
#include <functional>
#include <string>
#include <vector>
int main(int argc, char** argv)
{
std::vector<std::string> values;
values.push_back("test1");
values.push_back("test2");
values.push_back("test3");
std::vector<const char*> c_values(values.size());
std::transform(values.begin(), values.end(), c_values.begin(), std::mem_fn(&std::string::c_str));
std::transform(values.begin(), values.end(), c_values.begin(), std::bind(&std::string::c_str, std::placeholders::_1));
std::transform(values.begin(), values.end(), c_values.begin(), [](const std::string& str) { return str.c_str(); });
return 0;
}
When compiling with g++ (4.7.2), all three options compile and link fine. When compiling with clang, options 1 and 2 fail to link, producing:
$ clang -std=c++11 -stdlib=libc++ -lc++ stringtransform.cpp
Undefined symbols for architecture x86_64:
"std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::c_str() const", referenced from:
_main in stringtransform-ff30c1.o
ld: symbol(s) not found for architecture x86_64
I am finding I need to use the lambda version (option 3) if I want it to link correctly across platforms using both g++ and clang. Am I running into a linker bug or a hole in clang's C++11 support, or is there something wrong with how I'm invoking the mem_fn() and bind() versions?
EDIT:
Error still present on latest Xcode (6.3.2, with clang version 6.1.0:
$ clang -v
Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn)