Re: MSVC++ 2005 Express Ed. build error when mixing boost lexical_cast
and shared_ptr
* hsmit.home@gmail.com:
Thank you for the standard idiom:
std::string line ;
while ( std::getline( s, line ) ) {
d.push_back( line ) ;
}
It's very useful to know, and will help make a few areas of my code
more clear and STL like. My old mentor, Mr Wil Evers always taught me
to stay away from raw character pointers (char *). And now you've
shown me how - thank you. Tells you how little I still know about
STL...
You also write:
Because you then use a class defined in the global namespace.
Which means that the global namespace is drawn into ADL.
what does ADL stand for?
Argument Dependent Lookup, also known as Koenig lookup (after Andrew
Koenig). It essentially causes lookup in the namespaces of the actual
arguments to a function or operator call. This is how << usually works:
the definition is found in namespace std, because left arg is in std.
C++ is IMHO the most beautiful language out there. It's my favorite
and has taught me a great deal about programming well. But these type
of compiler errors whilst using standard libraries really scare me.
The following doubts enter my mind:
1) am I using a compliant compiler?
2) does the boost library have limitations?
3) am I writing inappropriate code?
These doubts are not a good thing.
[snip]
My previous post included the share_ptr.hpp, however, I just
discovered that the problem can be reproduced without including this
header.
I have now modified the code slightly. This compiles:
#include <boost/lexical_cast.hpp>
#include <iostream>
#include <vector>
#include <string>
namespace mytest {
// IF I PLACE: using namespace mytest; - THIS WORKS
typedef std::vector<std::string> stringvector;
//--------------------------------------------------------------
template<class T> std::ostream & operator<<(std::ostream& s, const
std::vector<T> & d) {
std::copy(d.begin(), d.end(),
std::ostream_iterator<std::string>(s, "\n"));
return s;
}
//--------------------------------------------------------------
template<class T> std::istream & operator>>(std::istream& s,
std::vector<T> & d) {
std::string line;
while (std::getline(s, line)) {
d.push_back(line);
}
return s;
}
}; // end mytest namespace
// THE TRICK TO GET THIS CODE TO COMPILE
using namespace mytest;
//--------------------------------------------------------------
int main (int argc, char ** argv) {
mytest::stringvector vecstr1;
vecstr1.push_back("hi");
vecstr1.push_back("there");
std::string str = boost::lexical_cast<std::string>(vecstr1);
std::cout << str << std::endl;
return 0;
}
However, if I remove the,
using namespace mytest;
It fails to compile. I have not tried to compile this in gc++ yet.
So, my next question is:
- why does placing the "mytest" namespace into the global namespace
solve this problem?
Because the typedef doesn't define a type that causes ADL: it doesn't
define a type, only an alternate name for an existing type. Ordinary
lookup finds the global definitions of << (brought there by your "using"
directive), but not those hidden down in your namespace. See my reply
to your original post.
Cheers, & hth.,
- Alf
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?