Skip to content

address-of-temporary idiom not allowed in C++ #18551

@pabigot

Description

@pabigot

Several places in Zephyr, particularly but not exclusively the Bluetooth stack, use an idiom of taking the address of a structure literal to pass complex configuration information into a function. Below is what is used to produce the BT_LE_ADV_CONN parameter passed to bt_le_adv_start() to identify the type of advertisement:

#define BT_LE_ADV_PARAM(_options, _int_min, _int_max) \
		(&(const struct bt_le_adv_param) { \
			.options = (_options), \
			.interval_min = (_int_min), \
			.interval_max = (_int_max), \
		 })
		 
#define BT_LE_ADV_CONN BT_LE_ADV_PARAM(BT_LE_ADV_OPT_CONNECTABLE, \
				       BT_GAP_ADV_FAST_INT_MIN_2, \
				       BT_GAP_ADV_FAST_INT_MAX_2)
...
bt_le_adv_start(BT_LE_ADV_CONN, ad, ARRAY_SIZE(ad), NULL, 0);

Taking the address of a temporary is an error when using C++: the correct idiom in that environment involves passing the temporary by reference. Allowing this idiom to be used requires -fpermissive, which is not a good solution.

Metadata

Metadata

Assignees

Labels

area: C++bugThe issue is a bug, or the PR is fixing a bugpriority: lowLow impact/importance bug

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions