
While fine-tuning my units code, I've run into something that's puzzling to me. Here's a minimal example: if I try to compile this program: #include <iostream> namespace boost { namespace units { typedef int time; typedef double length; } // namespace units } // namespace boost int main(void) { using namespace boost::units; length x = 10; time t = 2; return 0; } I get an error : "'time' not declared in this scope". However, if I fully qualify the namespace for "time", it compiles fine: #include <iostream> namespace boost { namespace units { typedef int time; typedef double length; } // namespace units } // namespace boost int main(void) { using namespace boost::units; length x = 10; boost::units::time t = 2; return 0; } This is on Xcode 2.4 (gcc 4.0.1). I assume that there is a definition of "time" somewhere in the standard headers that is polluting the global namespace. Has anyone run into this before? Any suggestions? Matthias

Matthias Schabel <boost@schabel-family.org> writes:
This is on Xcode 2.4 (gcc 4.0.1). I assume that there is a definition of "time" somewhere in the standard headers that is polluting the global namespace. Has anyone run into this before? Any suggestions?
Two words: "namespace alias" -- Dave Abrahams Boost Consulting www.boost-consulting.com

Matthias Schabel wrote:
This is on Xcode 2.4 (gcc 4.0.1). I assume that there is a definition of "time" somewhere in the standard headers that is polluting the global namespace. Has anyone run into this before? Any suggestions?
Yeah, I'm pretty sure I ran into this years ago, but it's foggy now what platform/compiler. It's at least partially the reason that date_time has 'ptime' instead of just 'time'. Anyway, Dave's answer is right unless you want to rename the class to something besides time. I'd guessing that what you are actually representing is a 'time_duration' as defined by ISO 8601, boost date_time, and others. That's a different concept than a 'time' which is a dimensionless point in the time continuum. Jeff

This is on Xcode 2.4 (gcc 4.0.1). I assume that there is a definition of "time" somewhere in the standard headers that is polluting the global namespace. Has anyone run into this before? Any suggestions?
Yeah, I'm pretty sure I ran into this years ago, but it's foggy now what platform/compiler. It's at least partially the reason that date_time has 'ptime' instead of just 'time'.
I guess I shouldn't say I'm "glad" to hear that I'm not the only one to run into this issue. I'm curious, though, if the standard has anything to say - is the standard library formally allowed to have symbols in the global scope?
Anyway, Dave's answer is right unless you want to rename the class to something besides time. I'd guessing that what you are actually representing is a 'time_duration' as defined by ISO 8601, boost date_time, and others. That's a different concept than a 'time' which is a dimensionless point in the time continuum.
I agree that namespace aliases are the obvious solution, if a little unsatisfying. You're right, of course - to be completely accurate, it is really a duration. I'm thinking of submitting the library for review after I get a little more feedback - at that point, it will probably be appropriate to consider the nomenclature at more length. As it is currently set up, time durations would be declared as quantity<T,time> which, I think, more or less implies that it is a duration... Matthias
participants (3)
-
David Abrahams
-
Jeff Garland
-
Matthias Schabel