này trông hợp lệ bởi draft C++11 standard, nếu chúng ta nhìn vào phần 5.2.3
chuyển đổi loại Explicit (ký hiệu chức năng) đoạn nói (nhấn mạnh tôi):
The expression T(), where T is a simple-type-specifier or typename-specifier for a non-array complete object type or the (possibly cv-qualified) void type, creates a prvalue of the specified type, whose value is that produced by value-initializing (8.5) an object of type T; no initialization is done for the void() case.[...]
từ ngữ là khá tương tự pre C++11.
này ổn trong một constexpr mặc dù phần 7.1.5
đoạn 3
nói:
The definition of a constexpr function shall satisfy the following constraints:
và bao gồm đạn này:
its return type shall be a literal type;
và trống không phải là một đen trong C++ 11 theo phần 3.9
đoạn , nhưng nếu chúng ta sau đó nhìn vào đoạn nó mang lại cho một ngoại lệ phù hợp với trường hợp này, nó nói:
If the instantiated template specialization of a constexpr function template or member function of a class template would fail to satisfy the requirements for a constexpr function or constexpr constructor, that specialization is not a constexpr function or constexpr constructor. [ Note: If the function is a member function it will still be const as described below. —end note ] If no specialization of the template would yield a constexpr function or constexpr constructor, the program is ill-formed; no diagnostic required.
Như Casey noted trong C++14 draft standardtrống là một đen, đây là phần 3.9
Loại đoạn nói:
A type is a literal type if it is:
và bao gồm:
— void; or
Nguồn
2013-12-09 19:15:53
Vâng, tôi biết bạn được phép có, nói, 'void foo() {} void bar() {return foo();}'. Tôi không chắc về điều này. – chris
Câu hỏi của bạn, cụ thể là gì? Về việc trả về 'void' (trong đó, BTW, luôn luôn hợp pháp trong C++)? Hoặc về sự kết hợp 'constexpr void'? – AnT
Có lẽ bạn nên giải thích những gì bạn nghĩ rằng lỗi nên được? –