extending 'import' using 'with'
Andrei Alexandrescu via Digitalmars-d
digitalmars-d at puremagic.com
Thu Apr 2 10:24:16 PDT 2015
On 4/2/15 2:52 AM, Dennis Ritchie wrote:
> On Wednesday, 1 April 2015 at 13:35:22 UTC, ketmar wrote:
>> people with Java/C/C++/etc. background tend to forget about the power of
>> metaprogramming: they have no such tool at hand, so they don't even think
>> about it.
>
> C++ with boost:
>
> -----
> #include <iostream>
>
> #include <boost/preprocessor/cat.hpp>
> #include <boost/preprocessor/seq/for_each.hpp>
>
> namespace a { namespace b {
> namespace x {
> int test1 = 1;
> }
> namespace y {
> int test2 = 2;
> }
> namespace z {
> int test3 = 3;
> }
> }
> }
>
> #define USE_NAMESPACES_IMPL( r, root, name ) \
> using namespace root :: name;
>
> #define USE_NAMESPACES( root, names ) \
> BOOST_PP_SEQ_FOR_EACH( USE_NAMESPACES_IMPL, root, names )
>
> USE_NAMESPACES( a::b, (x)(y)(z) )
>
> int main() {
>
> std::cout << test1 << '\n'; // 1
> std::cout << test2 << '\n'; // 2
> std::cout << test3 << '\n'; // 3
>
> return 0;
> }
> -----
> http://ideone.com/LncucP
Boost's preprocessor library is a scar on the landscape of C++. It has
wrecked such disasters on compilation times at work, that we're
considering disallowing it with a lint rule.
Andrei
More information about the Digitalmars-d
mailing list