Namespaces
Namespaces in C++ are used as scope specifiers for accessing type names, functions, variables, and so on. They allow us to more easily differentiate types and function names in large code bases that use many software components and where there are often similar identifiers.
In C, we usually add a prefix to types and functions to make it easier to differentiate, for example:
typedef struct hal_uart_stm32{
UART_HandleTypeDef huart_;
USART_TypeDef *instance_;
} hal_uart_stm32;
void hal_init();
uint32_t hal_get_ms();
In C++, we can use namespaces instead of C-style identifier prefixes to organize code in logical groups, as shown in the following example:
namespace hal {
void init();
std::uint32_t tick_count;
std::uint32_t get_ms() {
return tick_count;
}
class uart_stm32 {
private:
UART_HandleTypeDef huart_;
USART_TypeDef *instance_;
};
};
All members of the hal namespace are accessible unqualified from within the namespace. To access...