2016-05-27 17 views
8

Sau đây:Loại trả về theo sau có hợp pháp trong C++ 11 không?

auto (*f())() -> int; 

đưa ra một lỗi trong C++ chế độ 11 với Clang nói:

error: 'auto' return without trailing return type; deduced return types are a C++14 extension

nhưng biên dịch trong chế độ C++ 14. GCC biên dịch mà không cần phàn nàn ở cả hai chế độ với -Wall -Wextra -pedantic.

n3337 7.1.6.4/2 nói:

The auto type-specifier may appear with a function declarator with a trailing-return-type (8.3.5) in any context where such a declarator is valid.

8.3.5p2 nói về declarators chức năng nhưng tôi quá thiếu kinh nghiệm để con nó ra. Bất cứ ai có thể giải thích nếu nó hợp pháp trong C + + 11?

+1

Bạn đang cố gắng xác định loại trả về theo sau cho con trỏ hàm 'f' trả về? – user2357112

+0

Tôi đoán rằng GCC có quyền biên dịch nó. Cả Clang và GCC của Coliru đều cho rằng đó là 'int (* (*)())()'. – chris

+4

Bất kể tiêu chuẩn cho phép nó, nó cũng khá rõ ràng là một lỗi trình biên dịch. Một thông báo lỗi nói rằng một kiểu trả về đuôi được bỏ qua khi có một kiểu trả về đuôi là một thông báo lỗi xấu. – hvd

Trả lời

3

này một phần bao phủ bởi CWG 1725:

The treatment of a declaration like the following is not clear:

auto (*f())() -> int; // #1 

8.3.5 [dcl.fct] paragraph 2 appears to require determining the type of the nested declarator

auto (*f()); // #2 

which, because it does not have a trailing-return-type, would be ill-formed by (C++11) 7.1.6.4 [dcl.spec.auto]. (In C++14, an auto return type without a trailing-return-type is, of course, permitted.)

Rationale (September, 2013): The intent of the C++11 wording is that the requirement for a trailing return type applies only at the top level of the declarator to which auto applies, not to each possible recursive stage in the declarator processing. [..]

Bên cạnh đó, theo [dcl.fct]/2,

In a declaration T D where D has the form

      D1 (parameter-declaration-clause) [...] trailing-return-type

and the type of the contained declarator-id in the declaration T D1 is “derived-declarator-type-listT, T shall be the single type-specifierauto .

Như đã đề cập trong DR, T D1auto (*f()), đó là loại "chức năng của () trở về trỏ đến auto "(nghĩa là khớp với yêu cầu). Do đó mã của bạn là hợp lệ trong cả hai C++ 11 và 14, và loại của f là "chức năng của () trở về con trỏ đến chức năng của () trả lại int".

Các vấn đề liên quan