C + + standard 14 cancels decltype the return type of the derived function

Source: Internet
Author: User

Table of Contents
    • 1. Auto keyword not supported before c++11
    • 2. c++11 Support Auto keyword
      • 2.1. But the function return type cannot be inferred automatically
      • 2.2. Use Decltype to declare the return type
    • 3. c++14 let things go back to the simple
    • 4. Which C + + version should we use
1 Auto keyword not supported before c++11

The following code is not supported in c++11

Auto Add (int a, int b) {   int i = a + b;   return i;} int main (int argc,char * * argv) {  try {     std::cout << Add (+) << Std::endl;   } catch (Std::exceptio N Const &e) {     std::cerr << e.what () << Std::endl;  }}

The compiler clang++ 3.6 will error as follows:

.. /src/main.cc:13:1: Warning: ' auto ' type specifier is a c++11 extension [-wc++11-extensions]auto Add (int a, int b) {^ ... /src/main.cc:13:1: Error: ' Auto ' not allowed in function return Typeauto add (int a, int b) {^~~~
2 C++11 Support Auto keyword2.1 However, the function return type cannot be inferred automatically

Or the above example code, this compilation plus-std=c++11

.. /src/main.cc:13:1: Error: ' Auto ' return without trailing return type; deduced return types is a c++14 extensionauto add (int a, int b) {^1 error generated.

Here clang++ an error, saying that auto does not follow the description of the return type, or use the deduced return types provided by the C++14 standard.

2.2 Declaring return types using Decltype

Let's take a look at C++11 's recommended practice, and add a weird syntax to the back of the function.

Auto Add (int a, int b), decltype (A + b) {

Compiled by.

3 C++14 let things go back to the simple

Decltype (A + B) This syntax makes people feel less like C + +. But the 14 standard as a 11 standard quick follow-up, and pull things back. Decltype became a short-lived transition program. Now it can be completely abandoned. After adding-std=c++14 in the compile option, the following code passes the

Auto Add (int a, int b) {   int i = a + b;   return i;}
4 which C + + version should we use

Since the C++14 standard has been released, and the main compilers are already supported, so now is not the time to talk about c++11, but should directly use the C++14 standard. Here is the clang compiler support for the C + + standard document at the same time, here is my example project: [Email protected]:newlisp/cppwizard.git, using my own newLISP builder for C + + Compiling the code requires only modifying the compilation options of the Debug_config.lsp file.

(Set ' Compile-options "-g-std=c++14")

Updated C + + 17 or 1z is still in the process of standard-setting and is not currently recommended.

Author:dean

Created:2015-12-27 Day 10:23

Validate

C + + standard 14 cancels decltype the return type of the derived function

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.