Clang 和 GCC(也许还有 MSVC?)目前正在使用两步编译来实现其模块:
生成 BMI/CMI 但不生成目标文件的模块似乎有一些可能的用途,例如仅导出用于条件编译的类型或 constexpr 变量的模块。
据我从标准中可以理解,没有任何规定我必须生成/链接对象文件。所以我想知道我是否错过了关于使用这样的模块的一些明显的东西,以及我们是否期望工具支持这种“构建为模块,而不是构建为对象”工作流程?
我对在C++ 11和C++ 17中如何/为什么调用构造函数感到困惑.
#include <iostream>
using namespace std;
//---
template<typename T>
struct StructTest
{
public:
const T Var = -1;
//---
// constexpr StructTest() = delete; // 1
// constexpr StructTest() = default; // 2
// constexpr StructTest(const StructTest &Source) = delete; // 3
// constexpr StructTest(const StructTest &Source) = default; // 4
// constexpr StructTest(const StructTest &Source) {cout << "Copy" << endl;}; // 5
};
//---
StructTest<int> A{};
StructTest<int> A1{1};
StructTest<int> A2(A);
//---
int main(void)
{
return(0);
};
Run Code Online (Sandbox Code Playgroud)
所以当我取消注释某些行的组合(并使用带有clang的c …
c++ defaulted-functions deleted-functions list-initialization
我试图用 constexpr 变量和 ifs 替换我用来控制条件编译的预处理器 #define 和 #if/#ifdef。
是否可以声明 constexpr 变量,以便它们重现 #defines,因为它们不分配运行时存储空间,并且采用 1 的地址会导致编译时错误?
所以在标题中我想有类似的东西
namespace ExampleNamespace
{
enum class Platform : int {Darwin, Linux, Windows};
constexpr Platform BuildPlatform = Platform::Darwin; // Line A.
};
Run Code Online (Sandbox Code Playgroud)
在我想要的代码中
if constexpr (Platform::Darwin == BuildPlatform) // Line B.
{
cout << "Platform is Darwin" << endl;
}
else
{
cout << "Platform is not Darwin" << endl;
};
const Platform *const PlatformAddress = &BuildPlatform; // Line C.
const Platform &BuildPlatform2 = BuildPlatform; // …Run Code Online (Sandbox Code Playgroud)