Why do I get a “for declaration does not refer into a class, class template or class template partial specialization” on Clang and not GCC [duplicate]

I currently have this block of code which does not throw a compiler error on GCC, but throws this compiler error on clang:

error: nested name specifier ‘MyEnum::’ for declaration does not refer into a class, class template or class template partial specialization

I’m not actually too sure why this is the case. In the code example below, I have offered some potential solutions to not have the error being thrown, and my particular use case. Here’s the godolt link for a compiled example: https://godbolt.org/z/57aMreM3K

#include <stdexcept>

enum class MyEnum : int {
        MIN                  = 0,
        MAX                  = 1
};

class TestClass {
    public:
        explicit TestClass(const MyEnum type) { 
            throw std::runtime_error("Runtime Error");
        }
};

int main() {
    try {
        TestClass(MyEnum::MIN);
    } catch(...) { }
    
    // My usecase was to use google test to make sure there's no exception on construction
    // EXCEPT_NO_THROW(TestClass(MyEnum::MIN));

    // Potential solutions that compile for both GCC/Clang:
    // EXCEPT_NO_THROW(auto variable = TestClass(MyEnum::MIN));
    // EXCEPT_NO_THROW(TestClass((MyEnum)MyEnum::MIN));
}

3

Clang is correct here.

There is an ambiguity in the base C++ grammar for your statement

    TestClass(MyEnum::MIN);

Knowing only that TestClass is a valid type-name, but not performing any lookup aside from that, this could be either:

  1. An expression statement which consists of a functional style explicit cast expression that casts the result of the id-expression MyEnum::MIN to TestClass.

  2. A declaration with type-specifier TestClass, without any initializer and with parenthesized qualified declarator-id MyEnum::MIN. Note that in C and C++ declarators can always be wrapped in parentheses without change in meaning, e.g. int (x) = 5; is a valid declaration of a variable x of type int initialized to 5.

You want the first meaning. However, the ambiguity is always resolved in favour of a declaration based purely on the syntactic form of the statement and whether or not constituents are valid type-name. See [stm.ambig] in the standard (draft) for the exact rules.

In particular, the declaration interpretation is syntactically possible, but semantically ill-formed, because you can’t (re-)declare the enumerator MyEnum::MIN.

But since the semantics are not considered in the disambiguation, the interpretation as declaration must be chosen and the program is ill-formed as a consequence of the declaration being ill-formed.

That GCC doesn’t diagnose the issue is an open bug, see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=62116.


An easy way of preventing ambiguities of this kind from arising is to use curly braces instead of parentheses for functional-style explicit casts:

TestClass{MyEnum::MIN};

Depending on what you want to test this might not be a solution, because it can change the meaning of the expression.

Some other possibilities to disambiguate as an expression statement which do not affect the meaning of your expression are:

 (void)TestClass(MyEnum::MIN);
 static_cast<void>(TestClass(MyEnum::MIN));
 (TestClass(MyEnum::MIN));
 void(), TestClass(MyEnum::MIN);
 TestClass(MyEnum::MIN), void();

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật