从 C++20 开始,我们可以有:
constexpr bool is_little_endian = std::endian::native == std::endian::little;
Run Code Online (Sandbox Code Playgroud)
如果可用,我希望有执行此操作的代码,否则执行运行时检测。这可能吗?
也许代码看起来像:
template<bool b = std_endian_exists_v> constexpr bool is_little_endian;
template<> constexpr bool is_little_endian<true> = std::endian::native == std::endian::little;
template<> constexpr bool is_little_endian<false> = runtime_detection();
Run Code Online (Sandbox Code Playgroud)
我知道__cplusplus
通过预处理器进行测试是可能的,但是 C++20 中的编译器阶段在各个阶段都支持,因此这在任一方向都不是可靠的指标。
在C++ 1z中声明内存有效全局常量的最佳方法是什么,它不进行内部链接,因此在所有翻译单元中使用单个副本?
尽管在许多地方已经提到过,但我们没有任何单一的"最佳方法"问题和答案,所以在这里.以下是我找到相关问题的部分地方列表.
我在找为atleast所有基本基本类型的解决方案,如int
,double
,char
,std::string
.
编辑1: 最好的方式,我的意思是"记忆效率".我知道对于内部联系,每个翻译单元都会有多个副本,因此会占用内存.除此之外,如果我们能够实现快速的执行时间和编译时间,这将是伟大的,代码的美化(容易)并不重要.此外,我知道外部链接将增加获取时间,因为处理器可能会在运行时获得缓存未命中.但最新的C++语言是否支持所有这些的最佳方法?或者可能建议支持每个案例?
注意:对于std::string
全局常量,最佳方法是什么?是否存在可变性会对有任何影响常量性?
从这个问题来看,很明显auto
不能用作函数参数.我的问题是为什么允许返回类型,auto
但函数参数不是?
auto function(auto data)
{
//DOES something
}
Run Code Online (Sandbox Code Playgroud)
既然,进入c ++ 1z 会有很多好处,那么为什么不呢?auto
正确理解这些类型我遇到了麻烦:
对于:
int* j;
int** k;
Run Code Online (Sandbox Code Playgroud)
*j
&j
**j
*&j
*k
&k
**k
*&k
&*k
我的想法:
int**
- 双int指针?
解决了j
指针-地址什么类型?(十六进制值)
int***
?
指针,指向指针的地址j
?
int***
地址为双指针 k
int****
?
?