簡體   English   中英

noexcept依賴於成員函數的noexcept

[英]noexcept depend on noexcept of a member function

考慮:

class test {
    private:
        int n;

        int impl () const noexcept {
            return n;
        }

    public:
        test () = delete;
        test (int n) noexcept : n(n) {    }

        int get () const noexcept(noexcept(impl())) {
            return impl();
        }
};

GCC拒絕:

test.cpp:27:43: error: cannot call member function 'int test::impl() const' with
out object
   int get () const noexcept(noexcept(impl())) {

類似地:

test.cpp:27:38: error: invalid use of 'this' at top level
   int get () const noexcept(noexcept(this->impl())) {

test.cpp:31:58: error: invalid use of incomplete type 'class test'
   int get () const noexcept(noexcept(std::declval<test>().impl())) {
                                                          ^
test.cpp:8:7: error: forward declaration of 'class test'
 class test {

這是符合標准的預期行為還是GCC(4.8.0)中的錯誤?

對於其中的規則this可以用來改變的結果, 核心語言問題1207 ,其實還有另外一個原因,但在某種程度上也影響noexcept表達式。

前(后C ++ 03,但是當仍被寫C ++ 11), this是不允許的函數體外部使用。 所述noexcept表達式不是身體的一部分,所以this不能被使用。

之后, this可能是CV限定符序列后任何地方使用,並noexcept后表情出現,如你的問題的代碼清楚地說明。

看來此問題的GCC實施不完整,僅允許尾隨函數返回類型中的成員函數,但該標准的作用不止於此。 我建議將其報告為錯誤(如果以前尚未報告過)。 這已經在GCC bugzilla上報告為bug 52869

無論值多少錢,clang都接受C ++ 11模式下的代碼。

暫無
暫無

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

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