


Distinguish between C++ constant expressions, const, and constexpr (with code)
A constant expression is an expression whose value will not change and the calculation result can be obtained during the compilation process. It can be evaluated at compilation time expression.
Example 1:
#include <iostream> using namespace std; int main() { const int a1 = 10; // a1是常量表达式。 const int a2 = a1 + 20; // a2是常量表达式 int a3 = 5; // a3不是常量表达式 const int a4 = a3; // a4不是常量表达式,因为a3程序的执行到达其所在的声明处时才初始化,所以变量a4的值程序运行时才知道。但编译没问题! return 0; }
The above code can be compiled normally.
It shows that what is declared by const is not necessarily a constant expression!
The new C 11 standard stipulates that variables are allowed to be declared as constexpr type so that the compiler can verify whether the value of the variable is a constant expression. constexpr
Specifier declaration can obtain the value of a function or variable at compile time. A variable declared as constexpr must be a constant and must be initialized with a constant expression.
Example 2:
#include <iostream> using namespace std; int main() { const int a1 = 10; // a1是常量表达式。 const int a2 = a1 + 20; // a2是常量表达式 int a3 = 5; // a3不是常量表达式 constexpr int a4 = a3; // a4不是常量表达式,因为a3程序的执行到达其所在的声明处时才初始化,所以变量a4的值程序运行时才知道。编译报错! return 0; }
constexpr int a4 = a3; Compilation will report an error!
Example 3:
#include <iostream> using namespace std; int main() { const int a1 = 10; // a1是常量表达式。 const int a2 = a1 + 20; // a2是常量表达式 int a3 = 5; // a3不是常量表达式 const int a4 = a3; // a4不是常量表达式,因为a3程序的执行到达其所在的声明处时才初始化,所以变量a4的值程序运行时才知道。编译报错! char arr1[a2]; // 没问题 char arr2['y']; // 没问题,'y'的ASCII码为121,相当于 char arr2[121]; char arr3[a4]; // 编译报错,因为a4不是常量表达式 return 0; }
Related articles:
Share multiple examples of commonly used regular expressions in C
#PHP defines constants, the difference between const and define
The above is the detailed content of Distinguish between C++ constant expressions, const, and constexpr (with code). For more information, please follow other related articles on the PHP Chinese website!

C# is widely used in enterprise-level applications, game development, mobile applications and web development. 1) In enterprise-level applications, C# is often used for ASP.NETCore to develop WebAPI. 2) In game development, C# is combined with the Unity engine to realize role control and other functions. 3) C# supports polymorphism and asynchronous programming to improve code flexibility and application performance.

C# and .NET are suitable for web, desktop and mobile development. 1) In web development, ASP.NETCore supports cross-platform development. 2) Desktop development uses WPF and WinForms, which are suitable for different needs. 3) Mobile development realizes cross-platform applications through Xamarin.

The C#.NET ecosystem provides rich frameworks and libraries to help developers build applications efficiently. 1.ASP.NETCore is used to build high-performance web applications, 2.EntityFrameworkCore is used for database operations. By understanding the use and best practices of these tools, developers can improve the quality and performance of their applications.

How to deploy a C# .NET app to Azure or AWS? The answer is to use AzureAppService and AWSElasticBeanstalk. 1. On Azure, automate deployment using AzureAppService and AzurePipelines. 2. On AWS, use Amazon ElasticBeanstalk and AWSLambda to implement deployment and serverless compute.

The combination of C# and .NET provides developers with a powerful programming environment. 1) C# supports polymorphism and asynchronous programming, 2) .NET provides cross-platform capabilities and concurrent processing mechanisms, which makes them widely used in desktop, web and mobile application development.

.NETFramework is a software framework, and C# is a programming language. 1..NETFramework provides libraries and services, supporting desktop, web and mobile application development. 2.C# is designed for .NETFramework and supports modern programming functions. 3..NETFramework manages code execution through CLR, and the C# code is compiled into IL and runs by CLR. 4. Use .NETFramework to quickly develop applications, and C# provides advanced functions such as LINQ. 5. Common errors include type conversion and asynchronous programming deadlocks. VisualStudio tools are required for debugging.

C# is a modern, object-oriented programming language developed by Microsoft, and .NET is a development framework provided by Microsoft. C# combines the performance of C and the simplicity of Java, and is suitable for building various applications. The .NET framework supports multiple languages, provides garbage collection mechanisms, and simplifies memory management.

C# and .NET runtime work closely together to empower developers to efficient, powerful and cross-platform development capabilities. 1) C# is a type-safe and object-oriented programming language designed to integrate seamlessly with the .NET framework. 2) The .NET runtime manages the execution of C# code, provides garbage collection, type safety and other services, and ensures efficient and cross-platform operation.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Notepad++7.3.1
Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version
Chinese version, very easy to use

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
