2013-02-07 49 views
11

簡寫:如何在C++ 11中爲根(全局)名稱空間定義別名?它看起來像如何在C++ 11中別名全局名稱空間

namespace root_namespace = :: ;

,其中上面的裸體形式範圍解析操作是全局命名空間的一些手柄的佔位符。我在GCC Internals Manual中看到

... [編譯器]整個中間表示的根是變量global_namespace。這是在C++源代碼中使用::指定的名稱空間...全局名稱空間的名稱是::,即使在C++中全局名稱空間未命名。

PS EDIT:向受訪者最新的,我所附一個painfuly長形式以下長形式後,以解決一些關注,因爲它可以澄清一些事情。和追隨者,如果你看看我們,而不是彼此交談,挖掘


長篇:遵循其潛在用途的一個例子。如果不滿意,那麼是的,這是一個學術問題;看到這個後面的痛苦地長格式

想象一下,你的老闆在一天內駁船說:「我剛剛讀了一本關於後實證主義的書,擺脫了namespace ObjectiveReality。」在下面的代碼中,「所有」你必須做的是省略我標記爲/* -> */這個/* <- */的行。你可以目前做中間層次的嵌套;然而,我不確定如何定義全局範圍namespace current_authority以允許簡單省略第一個非全局命名空間。

#include <iostream> 
#include <iomanip> 


// ... 
using cat_is_alive = std::true_type ; // because I like cats 
using cat_is_alive = ::cat_is_alive ; // seems to work, see `g++ -v` below 
// ... 


// ASIDE: I originally had `namespace higher_authority = COMPILER ;` as a comment, but changed it for simpler chaining closure 

// The next two lines are the crux of my question... 

namespace higher_authority = global_namespace ; 
namespace current_authority = global_namespace ; // a.k.a. the naked :: 

// If the above two lines defined aliases for the (unnamed) global namespace, then the suggested elisions/replacements work... 

/* -> */ 
namespace ObjectiveReality { 
/* <- */ 
// Simplest fix: replace with `using ObjectiveReality = current_authority ;` 
// (Otherwise, a few other changes are necessary) 

    namespace higher_authority = current_authority ; 
    namespace current_authority = ObjectiveReality ; 

    using cat_is_alive = higher_authority::cat_is_alive ; 

    namespace EntangledObserver { 

     namespace higher_authority = current_authority ; 
     namespace current_authority = EntangledObserver ; 

     using cat_is_alive = higher_authority::cat_is_alive ; 
    } ; 
/* -> */ 
} ; 
/* <- */ 


int main(int argc, char** argv) { 

    std::cout 
     << "It is " 
     << ObjectiveReality::EntangledObserver::cat_is_alive::value 
     << " that the cat is alive." << std::endl ; 

    return 0 ; 
} 


// EOF 

如果編譯器信息需要:

$ g++ -std=c++11 -v 

Using built-in specs. 
COLLECT_GCC=g++ 
COLLECT_LTO_WRAPPER=/usr/lib/gcc/i686-linux-gnu/4.7/lto-wrapper 
Target: i686-linux-gnu 
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.7.2-11precise2' 
--with-bugurl=file:///usr/share/doc/gcc-4.7/README.Bugs 
--enable-languages=c,c++,go,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.7 
--enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib 
--without-included-gettext --enable-threads=posix 
--with-gxx-include-dir=/usr/include/c++/4.7 --libdir=/usr/lib 
--enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug 
--enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin 
--enable-objc-gc --enable-targets=all --disable-werror --with-arch-32=i686 
--with-multilib-list=m32,m64 --with-tune=generic --enable-checking=release 
--build=i686-linux-gnu --host=i686-linux-gnu --target=i686-linux-gnu 
Thread model: posix 
gcc version 4.7.2 (Ubuntu/Linaro 4.7.2-11precise2) 

痛苦LONG FORM:如約 「從嵌套命名開始,」 請注意,「HOME一些答案的響應! 「無法訪問,並且我可能沒有在團隊中親自挑選命名空間的奢侈品。

// 
// alice.cpp 
// 


#include <iostream> 
#include <type_traits> 


///// 
///// The Setup 
///// 

// 
// One-and-a-half macros 
// 

// BOO! Get rid of this case! 
#define ENABLE_SUBSPACE_1(namespace_name) \ 
    namespace namespace_name { \ 
    namespace last_namespace = ::namespace_name ; \ 
    namespace this_namespace = ::namespace_name ; 

// YEAH! Instead, define 'namespace this_namespace = :: ;' and then... 
#define ENABLE_SUBSPACE(namespace_name) \ 
    namespace namespace_name { \ 
    namespace last_namespace = this_namespace ; \ 
    namespace this_namespace = last_namespace::namespace_name ; 

// 
// Some characters 
// 

struct dorothy { 
    static constexpr auto obvious_statement = "There's no place like " ; 
} ; 

struct rabbit { 
    template< typename T > 
    static constexpr char const* says(T) { 
     return T::value ? "I'm late!" : "I'm late, but I ditched that addled girl." ; 
    } 
} ; 

struct alice { 

    using blue_pill = std::false_type ; 
    static constexpr auto where_am_i = "HOME!" ; 
} ; 


///// 
///// The Central Structure 
///// 

ENABLE_SUBSPACE_1(oxford_england) // { 

    using has_strangers_with_candy = std::true_type ; 

    struct alice { 
     using blue_pill = this_namespace::has_strangers_with_candy ; 
     static constexpr auto where_am_i = "Uncle Charles' picnic blanket." ; 
    } ; 

ENABLE_SUBSPACE(rabbit_hole) // { 
    struct rabbit { using is_late = typename alice::blue_pill ; } ; 
ENABLE_SUBSPACE(rabbit_hole) // { 
    struct rabbit { using is_late = typename alice::blue_pill ; } ; 
ENABLE_SUBSPACE(rabbit_hole) // { 
    struct rabbit { using is_late = typename alice::blue_pill ; } ; 
    using has_strangers_with_candy = std::false_type ; ///// Different... 
ENABLE_SUBSPACE(rabbit_hole) // { 
    struct rabbit { using is_late = typename alice::blue_pill ; } ; 
ENABLE_SUBSPACE(rabbit_hole) // { 
    struct rabbit { using is_late = typename alice::blue_pill ; } ; 
    struct alice { ///// Different... 
     using blue_pill = has_strangers_with_candy ; 
     static constexpr auto where_am_i = "needing a fix." ; 
    } ; 
ENABLE_SUBSPACE(rabbit_hole) // { 
    struct rabbit { using is_late = typename alice::blue_pill ; } ; 
    struct alice : last_namespace::alice { ///// Different... 
     static constexpr auto where_am_i = "an empty rabbit hole." ; 
    } ; 
} ; // END rabbit_hole 
} ; // END rabbit_hole 
} ; // END rabbit_hole 
} ; // END rabbit_hole 
} ; // END rabbit_hole 
} ; // END rabbit_hole 
} ; // END oxford_england 


///// 
///// Snarky Output 
///// 

int main(int argc, char** argv) { 

    std::cout << std::endl 
     << dorothy::obvious_statement 
     << alice::where_am_i 
     << std::endl ; // There's no place like HOME! 

    std::cout 
     << dorothy::obvious_statement 
     << oxford_england::rabbit_hole::rabbit_hole::rabbit_hole::rabbit_hole::rabbit_hole::alice::where_am_i 
     << std::endl ; // There's no place like needing a fix. 

    std::cout 
     << dorothy::obvious_statement 
     << oxford_england::rabbit_hole::rabbit_hole::rabbit_hole::rabbit_hole::rabbit_hole::rabbit_hole::alice::where_am_i 
     << std::endl ; // There's no place like an empty rabbit hole. 

    std::cout << std::endl 
     << rabbit::says(
      oxford_england:: 
       rabbit_hole:: 
        rabbit_hole:: 
         rabbit_hole:: 
          rabbit_hole:: 
           rabbit_hole::rabbit::is_late() 
     ) << std::endl ; // I'm late! 
    std::cout 
     << rabbit::says(
      oxford_england:: 
       rabbit_hole:: 
        rabbit_hole:: 
         rabbit_hole:: 
          rabbit_hole:: 
           rabbit_hole:: // NOTE : alice::blue_pill is false_type 
            rabbit_hole::rabbit::is_late() // ... not this time ; Alice is crashing. 
     ) << std::endl ; // I'm late, but I ditched that addled girl. 

    std::cout << std::endl 
     << dorothy::obvious_statement 
     << oxford_england:: 
       rabbit_hole:: // 1 
        rabbit_hole:: // 2 
         rabbit_hole:: // 3 
          rabbit_hole:: // 4 
           rabbit_hole:: // 5 
            rabbit_hole:: // rabbit_hole #6 
           last_namespace:: // rabbit_hole #5 
          last_namespace:: // rabbit_hole #4 
         last_namespace:: // rabbit_hole #3 
        last_namespace:: // rabbit_hole #2 
       last_namespace:: // rabbit_hole #1 
      last_namespace::alice::where_am_i // oxford_england 
     << std::endl ; // There's no place like Uncle Charles' picnic blanket. 
    std::cout 
     << dorothy::obvious_statement 
     << oxford_england:: 
       rabbit_hole:: 
        rabbit_hole:: 
         rabbit_hole:: 
          rabbit_hole:: 
           rabbit_hole:: 
            rabbit_hole:: 
           last_namespace:: 
          last_namespace:: 
         last_namespace:: // 3 
        last_namespace:: // 2 
       last_namespace:: // 1 
      last_namespace:: // oxford 
     last_namespace::alice::where_am_i // not the global namespace! 
     << ".. but I'd rather be " << ::alice::where_am_i // the global namespace. 
     << std::endl ; // There's no place like Uncle Charles' picnic blanket... but I'd rather be HOME! 

    std::cout << std::endl ; 
    return 0 ; 
} 


///// 
///// EOF 
///// 


/* Compiled with: 
    `g++ -std=c++11 -o alice alice.cpp` 
*/ 

/* Output of `alice` : 

There's no place like HOME! 
There's no place like needing a fix. 
There's no place like an empty rabbit hole. 

I'm late! 
I'm late, but I ditched that addled girl. 

There's no place like Uncle Charles' picnic blanket. 
There's no place like Uncle Charles' picnic blanket... but I'd rather be HOME! 

*/ 
+0

英文請。這個詞是「冒號」。 –

+0

爲什麼不能在全局範圍中添加'使用名稱空間ObjectiveReality;'? –

+2

+1對於C++命名空間的一個奇怪哲學。 – nneonneo

回答

2

我不認爲你可以別名全局命名空間。

該標準定義了一個命名空間別名與::叫了明確而不是把它像只是一個命名空間名稱:

7.3.2 Namespace alias 
A namespace-alias-definition declares an alternate name for a namespace according to the following grammar: 
namespace-alias: 
    identifier 
namespace-alias-definition: 
    namespace identifier = qualified-namespace-specifier ; 
qualified-namespace-specifier: 
    ::_opt nested-name-specifier_opt namespace-name 

注意::是選擇(可選),但名稱空間名稱不是。

但是,難道你不能僅僅使用全局命名空間以外的其他命名空間作爲你的命名空間棧的開始,並且仍然遵循你所概述的模式嗎?

另外請注意,您的例子事實上確實需要當您刪除ObjectiveReality幾個其他行編輯:

namespace current_authority = ObjectiveReality ; 

和:

<< ObjectiveReality::EntangledObserver::cat_is_alive::value 

這裏是你的榜樣看起來像使用命名空間「東西」而不是將全局命名空間作爲命名空間堆棧的根。

#include <iostream> 
#include <iomanip> 


// ...                                                                                          
namespace something { 
    using cat_is_alive = std::true_type ; // because I like cats                                                                            
} 
using namespace something; 
// ...                                                                                          


// ASIDE: I originally had `namespace higher_authority = COMPILER ;` as a comment, but changed it for simpler chaining closure                                                            

// The next two lines are the crux of my question...                                                                              

namespace higher_authority = something; 
namespace current_authority = something;                                                                           

// If the above two lines defined aliases for the (unnamed) global namespace, then the suggested elisions/replacements work...                                                            

/* -> */ 
namespace ObjectiveReality { 
    /* <- */ 
    // Simplest fix: replace with `using ObjectiveReality = current_authority ;`                                                                        
    // (Otherwise, a few other changes are necessary)                                                                              

    namespace higher_authority = current_authority ; 
    namespace current_authority = ObjectiveReality ; 

    using cat_is_alive = higher_authority::cat_is_alive ; 

    namespace EntangledObserver { 

    namespace higher_authority = current_authority ; 
    namespace current_authority = EntangledObserver ; 

    using cat_is_alive = higher_authority::cat_is_alive ; 
    } ; 
    /* -> */ 
} ; 
/* <- */ 


int main(int argc, char** argv) { 

    std::cout 
    << "It is " 
    << ObjectiveReality::EntangledObserver::cat_is_alive::value 
    << " that the cat is alive." << std::endl ; 

    return 0 ; 
} 


// EOF 
+0

你叫我出去。我實際上重寫了'cat_is_alive'作爲一種試圖混淆我的罪行的類型;我將不得不考慮你的候選人的答案,謝謝你的例子和語法!同時:是否存在_disallow_全局命名空間別名的合法存在理由? –

+0

仍然在研究 - 但是,就像在桶裏有一個洞。從上面的Soverman開始,「......你不能僅僅使用全局命名空間以外的其他命名空間作爲你的命名空間棧的開始,並且仍然遵循你所概述的模式嗎?」我的宏偉計劃是修復n = 1的基本案例。 n => n + 1很好,但是抽象層次不是解決方法,是嗎? –

+2

我不認爲有任何存在的理由來阻止別名全局命名空間。據我所知,這就是語言的定義方式。有時候C++有點荒唐[ist]。您能否解釋爲什麼使用命名空間而不是全局命名空間,因爲n = 1的基本情況對您的方案無效?我沒有遵循你的抽象層次的關注。 – Soverman

2

我不知道的方式,直接別名默認的命名空間,但作爲一種解決方法您可以使用宏(假設你願意生活在宏的名稱與符號無關碰撞)。

#define root_namespace 

用法:

root_namespace::cat_is_alive 

由於鳴叫鴨正確地指出,這個黑客不會using條款的工作。

+4

這很聰明,但是......使用namespace root_namespace; –

相關問題