簡體   English   中英

在非會員function中將嵌套名稱帶入scope

[英]Bring nested name into scope in non-member function

我有一個結構 B,其中包含類型anchor_point的聲明作為嵌套名稱。 如何使用using -指令將anchor_point帶入另一個 function 中的 scope? 我基本上想按原樣訪問類型而不對其進行限定(就像從成員函數中一樣)。 我嘗試了以下(見評論):

代碼

#include <memory>
#include <iostream>

struct B
{
    struct anchor_point
    {
        int a_;
    };
    int x;
    int y;
};

int main()
{
    // Here's what I want - doesn't compile
    // using B;
    // anchor_point ap1{5};

    // This gets laborious if there are dozens of types inside of B
    B::anchor_point ap2{5};

    // This is even more laborious
    using ap_t = B::anchor_point;
    ap_t ap3{5};

    std::cout << ap2.a_ << ", " << ap3.a_ << "\n";
}

這個例子很簡單,但假設我在結構中聲明了幾十個這樣的類型,我並不總是想輸入B::type ,我該怎么做?

正如你所展示的,你可以做到

using anchor_point = B::anchor_point;

對每個相關成員重復。 這必須在 scope 中只出現一次,其中包含您對要涵蓋的成員的所有使用。

沒有其他方法,特別是沒有等同於using namespace的名稱空間,這使得所有成員對不合格的名稱查找可見。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM