C++學習筆記九:值,常量,常表示式和常初始化

Sternenhimmel1997發表於2023-12-13

1. 值:

Literal: Data that is directly represented in code without going through some other variable stored in memory.

值: 直接在程式碼中表示的資料,無需佔用記憶體,而是直接在二進位制的可執行檔案中產生。

1.1 整數值

//Literal types : u and l combinations for unsigned and long.(大小寫都可)
unsigned char unsigned_char {53u};// 555U would fail because of narrowing

//2 Bytes
short short_var {-32768} ; //  No special literal type for short)
short int short_int {455} ; //  No special literal type for short
signed short signed_short {122}; // No special literal type for short
signed short int signed_short_int {-456}; // No special literal type for short
unsigned short int unsigned_short_int {5678U };

// 4 Bytes
const int int_var {55} ;            //
signed signed_var {66};             //
signed int signed_int {77};         //
unsigned int unsigned_int {555U};       //

//4 or 8 Bytes
long long_var {88L}; // 4 OR 8 Bytes
long int long_int {33L};
signed long signed_long {44l};
signed long int signed_long_int {44l};
unsigned long int unsigned_long_int {555ul};

//8 Bytes
long long long_long {888ll};// 8 Bytes
long long int long_long_int {999ll};
signed long long signed_long_long {444ll};
signed long long int signed_long_long_int{1234ll};

//Grouping Numbers : C++14 and onwards
unsigned int prize  {1'500'00'0u};
std::cout << "The prize is : " << prize << std::endl;
std::cout << " signed_long_long_int : " << signed_long_long_int << std::endl;

//Narrowing errors
//Possible narrowing errors are cought by the braced initializer method.
//Assignment and functional don't catch that.
//unsigned char distance {555u}; //Error [0~255]
//unsigned int game_score {-20}; //Error

 

1.2 十六進位制賦值:

//With number systems - Hex : prefix with 0x
unsigned int hex_number{ 0x22BU}; // Dec 555
int hex_number2 {0x400};// Dec 1024
std::cout << std::hex <<  "The hex number is : " << hex_number << std::endl;
std::cout << std::dec <<  "The hex number2 is : " << hex_number2 << std::endl;
//review: direct output only gives devimal number, use std::hex to get hex output
//Representing colors with hex
int black_color {0xffffff};
std::cout << "Black color is : " << std::dec << black_color << std::endl;

 

1.3 八進位制賦值:

//Octal literals : prefix with 0
int octal_number {0777u}; // 511 Dec
std::cout << std::oct << "The octal number is : " << octal_number << std::endl;
//!!BE CAREFUL NOT TO PREFIX YOUR INTEGERS WITH 0 IF YOU MEAN DEC
int error_octal {055};// This is not 55 in memory , it is 45 dec
std::cout << "The erronous octal number is : " << error_octal << std::endl;

 

1.4 二進位制賦值

//Binary literals
unsigned int binary_literal {0b11111111u};// 255 dec
std::cout << "The binary literal is : " << binary_literal << std::endl;

 

1.5 其他型別賦值

char char_literal {'c'};
int number_literal {15};
float fractional_literal {1.5f}; //不加f預設只是double型別
std::string string_literal {"Hit the road"};

 

2. 常量

const: A read only variable. Cannot assign data to it.

常量:const修飾後成為一個只讀值,不能二次賦值。

CPU並沒有常量的概念,是程式層在constant對變數修飾後導致變數只讀的屬性。

一般用於定義在整個程式執行週期內都不想被改變的量。常量在宣告的時候必須賦值。

const int age {34};
const float height {1.67f};

//age = 55; // Can't modify
//height = 1.8f;

 

3. 常表示式

constexpr: A constant that has the potential to be evaluated at compile time

3.1 程式的產生過程:

IDE Code -> Compile time(從程式碼生成二進位制可執行檔案) -> Run time

如果可能的話,把複雜的計算放到編譯中去做(Compile time computations),一旦編譯完成,在執行時可以直接呼叫編譯生成的結果。c++11之後引入的特性。

可執行二進位制檔案的一般執行過程:

initialization->Constexpr computation->Runtime computation

改進後:

initialization->Runtime computation

3.2 舉例:

constexpr int SOME_LIB_MAJOR_VERSION {1237};
constexpr int eye_count {2};
constexpr double PI {3.14};
//eye_count = 4;  //error
std::cout << "eye count : " << eye_count << std::endl;
std::cout << "PI : " << PI << std::endl;

// int leg_count {2}; // Non constexpr
// leg_count is not known at compile time
// constexpr int arm_count{leg_count}; // Error, cannot initialize a compile
// time variable with a runtime variable

constexpr int room_count{10};
constexpr int door_count{room_count};// OK
const int table_count{5};
constexpr int chair_count{ table_count * 5};// Works

static_assert( SOME_LIB_MAJOR_VERSION == 123); //compile time check
// int age = 5;
// static_assert( age == 5); //不能檢查非constant變數

std::cout << "App doing its thing..." << std::endl;

 

4. 常初始化(C++20特性)

constinit: A variable that should be initialized with a constant or literal at compile time

指定變數必須擁有靜態初始化,即零初始化與常量初始化,否則程式非良構。這種變數必須是靜態的或者是全生命週期的variables with static or thread storage duration

什麼是靜態變數:When you declare a variable in a function, the static keyword specifies that the variable retains its state between calls to that function. When you declare a data member in a class declaration, the static keyword specifies that one copy of the member is shared by all instances of the class.

可以幫助解決在主函式外的初始化問題。必須初始化。constconstinit可以結合使用,constconstexpr不可以結合使用。const init不說明變數是常量,只是說明編譯器強制其在編譯過程中初始化。

#include <iostream>

const int val1 {33};
constexpr int val2{34};
int val3 {35}; // Run time value

constinit int age = 88; // This is initialized at compile time
const constinit int age1 {val1}; // const and constinit can be combined
constinit int age2 {age1}; // Initializing with age would lead to a compiler error
                                 // age is not const
//constinit int age3 {val3}; // Error : val3 is evaluated at run time
                           // can't const initialize age3                           
const constinit double weight {33.33};
//constexpr constinit double scale_factor{3.11};// Can't combine constexpr and constinit

int main(){
    //constinit double height{1.72}; //compiler error, not static or thread storage duration
    std::cout << "age : " << age << std::endl;
    std::cout << "age1 : " << age1 << std::endl;
    std::cout << "age2 : " << age2 << std::endl;
    age =33; // Can change a const init variable
    std::cout << "age : " << age << std::endl;
    //Combining const and constinit
    std::cout << "weight : " << weight << std::endl;
    //weight = 44.44; // Compiler error
    return 0;
}

 

相關文章