list Initialization using {}


If an initializer is specified for an object, that initializer determines the initial value of an object.
An initializer can use one of four syntactic styles:
X a1 {v};
X a2 = {v};
X a3 = v;
X a4(v);
Of these, only the first can be used in every context, and I strongly recommend its use. It is clearer
and less error-prone than the alternatives. However, the first form (used for a1 ) is new in C++11, so
the other three forms are what you find in older code. The two forms using = are what you use in
C. Old habits die hard, so I sometimes (inconsistently) use = when initializing a simple variable
with a simple value. For example:
int x1 = 0;
char c1 = 'z';
However, anything much more complicated than that is better done using {} . Initialization using {} ,
list initialization, does not allow narrowing (§iso.8.5.4). That is:
• An integer cannot be converted to another integer that cannot hold its value. For example,
char to int is allowed, but not int to char .
• A floating-point value cannot be converted to another floating-point type that cannot hold its
value. For example, float to double is allowed, but not double to float .
• A floating-point value cannot be converted to an integer type.
• An integer value cannot be converted to a floating-point type.
For example:
void f(double val, int val2)
{
int x2 = val;
// if val==7.9, x2 becomes 7
char c2 = val2;
// if val2==1025, c2 becomes 1160
Types and Declarations
Chapter 6
int x3 {val};
char c3 {val2}; // error : possible truncation
// error : possible narrowing
char c4 {24};
char c5 {264}; // OK: 24 can be represented exactly as a char
// error (assuming 8-bit chars): 264 cannot be represented as a char
int x4 {2.0}; // error : no double to int value conversion
// ...
}
See §10.5 for the conversion rules for built-in types.
There is no advantage to using {} initialization, and one trap, when using auto to get the type
determined by the initializer. The trap is that if the initializer is a {} -list, we may not want its type
deduced (§6.3.6.2). For example:
auto z1 {99};
auto z2 = 99;
// z1 is an initializer_list<int>
// z2 is an int
So prefer = when using auto .
It is possible to define a class so that an object can be initialized by a list of values and alterna-
tively be constructed given a couple of arguments that are not simply values to be stored. The clas-
sical example is a vector of integers:
vector<int> v1 {99};
vector<int> v2(99);
// v1 is a vector of 1 element with the value 99
// v2 is a vector of 99 elements each with the default value 0
I use the explicit invocation of a constructor, (99) , to get the second meaning. Most types do not
offer such confusing alternatives – even most vector s do not; for example:
vector<string> v1{"hello!"};
vector<string> v2("hello!");
// v1 is a vector of 1 element with the value "hello!"
// error : no vector constructor takes a string literal
So, prefer {} initialization over alternatives unless you have a strong reason not to.
The empty initializer list, {} , is used to indicate that a default value is desired. For example:
int x4 {};
double d4 {};
char∗ p {};
vector<int> v4{};
string s4 {};
// x4 becomes 0
// d4 becomes 0.0
// p becomes nullptr
// v4 becomes the empty vector
// s4 becomes ""

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
The initialization of UART4 can vary depending on the microcontroller or development board being used. However, here is an example of how to initialize UART4 on a STM32F4 Discovery board using CubeMX and HAL libraries: 1. Open CubeMX and create a new project for your microcontroller or development board. 2. In the "Pinout & Configuration" tab, select "UART4" from the peripherals list. 3. Enable the "Asynchronous" mode and set the baud rate to the desired value. 4. Set the word length, stop bits, and parity according to your application's requirements. 5. Configure the GPIO pins for the UART4 peripheral by selecting the appropriate pins in the "Pinout" tab. 6. Generate the code by clicking on the "Generate" button. 7. Open the generated code and locate the "MX_UART4_Init" function. 8. In this function, you will find the initialization code for UART4. It will look something like this: ``` huart4.Instance = UART4; huart4.Init.BaudRate = 115200; huart4.Init.WordLength = UART_WORDLENGTH_8B; huart4.Init.StopBits = UART_STOPBITS_1; huart4.Init.Parity = UART_PARITY_NONE; huart4.Init.Mode = UART_MODE_TX_RX; huart4.Init.HwFlowCtl = UART_HWCONTROL_NONE; huart4.Init.OverSampling = UART_OVERSAMPLING_16; if (HAL_UART_Init(&huart4) != HAL_OK) { Error_Handler(); } ``` 9. This code initializes UART4 with a baud rate of 115200, 8 data bits, 1 stop bit, no parity, and both transmit and receive modes enabled. 10. You can now use the HAL functions to send and receive data over UART4. For example, to transmit a string over UART4, you can use the following code: ``` char* str = "Hello, World!"; HAL_UART_Transmit(&huart4, (uint8_t*)str, strlen(str), HAL_MAX_DELAY); ``` This will send the string "Hello, World!" over UART4.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值