所以我昨天安装了Visual Studio 2017.我还安装了支持VS 2017的CMake 3.7.2.
我的VS安装与Game development with C++工作流程+一些其他组件:

我还添加了CMake的东西(但我认为我甚至不需要它 - 因为我使用CMake作为独立工具来生成VS解决方案)和MSBuild(我msbuild.exe甚至在添加该组件之前 - 所以没有确定该附加组件究竟做了什么).
使用VS 2015,我能够cmake .从正常的命令提示符运行以获得解决方案.
随着VS 2017工作流程的变化 - 我已经阅读了微软的这篇文章.
所以我尝试了以下方法:
Developer Command Prompt for VS 2017,从那里跑了cmake . -G "NMake Makefiles".然后cmake --build .正确运行编译的一切.当我在提示中尝试以下操作时:cmake . -G "Visual Studio 15 2017 Win64"强制创建解决方案时出现以下错误:
-- The C compiler identification is unknown
-- The CXX compiler identification is unknown
CMake Error at CMakeLists.txt:3 (project):
No CMAKE_C_COMPILER could …Run Code Online (Sandbox Code Playgroud)以下代码使用MSVC(2015 Update 3)生成警告 - 包含/W4:
const bool& a = true;
const int& b = 1;
if(a == b)
Run Code Online (Sandbox Code Playgroud)
C4805: '==': unsafe mix of type 'const bool' and type 'const int' in operation
但没有参考,它编译干净.
const bool a = true;
const int b = 1;
if(a == b)
Run Code Online (Sandbox Code Playgroud)
为什么?
编辑:
刚刚测试没有const
bool a = true;
int b = 1;
if(a == b)
Run Code Online (Sandbox Code Playgroud)
警告再次出现......
编辑2:
在Debug中编译...虽然我确实需要C4127: conditional expression is constant在const noref情况下保持沉默......
编辑3:
以下是3种情况的拆卸:
const ref
0113BA92 in al,dx
0113BA93 …Run Code Online (Sandbox Code Playgroud) 所以我在2个单独的翻译单元中有这个代码:
// a.cpp
#include <stdio.h>
inline int func() { return 5; }
int proxy();
int main() { printf("%d", func() + proxy()); }
// b.cpp
inline int func() { return 6; }
int proxy() { return func(); }
Run Code Online (Sandbox Code Playgroud)
正常编译时,结果是10.当使用-O3(内联)编译时,我得到了11.
我已经明确做了ODR违规func().
当我开始将不同dll的来源合并为更少的dll时,它就出现了.
我试过了:
-Wodr(需要-flto)-detect-odr-violationsASAN_OPTIONS=detect_odr_violation=1在使用地址清理程序运行检测二进制文件之前进行设置.据称,Asan可以捕获其他ODR违规行为(具有不同类型或类似内容的全球变量......)
这是一个非常令人讨厌的C++问题,我很惊讶没有可靠的工具来检测它.
也许我误用了我试过的一种工具?或者是否有不同的工具?
编辑:
即使我使两个实现完全func()不同,这个问题仍然没有引起注意,因此它们不会被编译为相同数量的指令.
这也会影响类体内定义的类方法 - 它们是隐式内联的.
// a.cpp
struct A { int data; A() : data(5){} …Run Code Online (Sandbox Code Playgroud) 在编译翻译单元时,编译器正在进行大量优化 - 内联,常量折叠/传播,别名分析,循环展开,死代码消除以及许多其他我都没有听说过的优化.在多个翻译单元之间使用LTO/LTCG/WPO时,是否所有这些都完成了,或者只是它们的一个子集(或变体)(我听说过内联)?如果没有完成所有优化,我会认为统一构建优于LTO(或者当存在多于1个统一源文件时可以使用它们).
我的猜测是它不一样(统一构建具有完整的优化集),并且它在编译器之间变化很大.
关于每个编译器的lto的文档没有准确地回答这个问题(或者我没理解它).
由于lto涉及在理论上将中间表示保存在目标文件中,LTO可以进行所有优化......对吗?
请注意,我不是在询问构建速度 - 这是一个单独的问题.
编辑:我最感兴趣的是gcc/llvm.
我正在查看编译器提供的各种STL头文件,我无法想象开发人员实际上是手工编写所有这些代码.所有的宏和奇怪的变量和类的名称 - 他们必须记住所有这些!似乎容易发生错误.标题的一部分是否由某些文本预处理或生成产生?
为什么以下代码编译:
template<typename T>
void foo(T in) { bar(in); }
struct type{};
void bar(type) {}
int main() { foo(type()); }
Run Code Online (Sandbox Code Playgroud)
如果以下情况不符合:
template<typename T>
void foo(T in) { bar(in); }
void bar(int) {}
int main() { foo(42); }
Run Code Online (Sandbox Code Playgroud)
使用GnuC++ 7进行编译:
a.cpp: In instantiation of 'void foo(T) [with T = int]':
a.cpp:9:20: required from here
a.cpp:2:21: error: 'bar' was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation [-fpermissive]
void foo(T in) { bar(in); …Run Code Online (Sandbox Code Playgroud) c++ templates language-lawyer name-lookup argument-dependent-lookup
给出以下代码:
constexpr int omg() { return 42; }
const int a = omg(); // NOT guaranteed to be evaluated at compile time
constexpr const int a = omg(); // guaranteed to be evaluated at compile time
Run Code Online (Sandbox Code Playgroud)
有没有办法强制在编译时评估某些东西而不将其分配给constexpr(或者在编译时上下文中,比如模板参数ot enum shenanigans)?
像这样的东西:
const int a = force_compute_at_compile_time(omg());
Run Code Online (Sandbox Code Playgroud)
也许这样的东西(不编译 - 我还没有太多进入constexpr):
template<typename T> constexpr T force_compute_at_compile_time(constexpr const T& a) { return a; }
Run Code Online (Sandbox Code Playgroud) 有没有办法获得一个类的字段数?
struct Base {
char a;
int b;
};
struct Derived : Base {
std::string c;
};
static_assert(num_fields<Base>::value == 2);
static_assert(num_fields<Derived>::value == 1);
Run Code Online (Sandbox Code Playgroud)
我发现了这个问题,但它已经过时了 - 我希望可以用C++ 14/17拼接一些东西(毕竟我们现在有类似magic_get的东西- 可能是它的一些子集......?)
编辑: - 编译器钩子也可以工作,即使它只适用于MSVC或GCC或Clang - 我使用全部3.
std::swap 以这种方式声明:
template <class T> void swap (T& a, T& b)
noexcept (is_nothrow_move_constructible<T>::value &&
is_nothrow_move_assignable<T>::value);
Run Code Online (Sandbox Code Playgroud)
如果我在我的程序中禁用异常(比如使用-fno-exceptionsfor g ++)std::swap,如果它们是移动启用的,那么无论它们是否为noexcept,我都会使用自定义类型的移动操作?
编辑:后续问题:
在意识到std :: swap将始终使用移动时,如果我的类型有它们,我真正的问题是会发生什么样的特征is_nothrow_move_assignable<>?
将std::vector重新分配的时候,如果我的类型始终使用移动noexcept(true)的移动操作?
我正在编写测试框架,在main()中我返回失败测试的数量.例如:
int main() {
int failedTests = runTests();
return failedTests;
}
Run Code Online (Sandbox Code Playgroud)
这个"int"溢出了吗?我能%ERROR_LEVEL%要== 0当我真正回来的东西比0有什么不同?它取决于主机操作系统吗?通常的最大值是多少?我可以确定整数<32768(2 ^ 16 - 一个短)总是适合吗?
编辑:
我有sys.exit使用0-127范围(几乎)的python问题所以我现在小心了
c++ ×10
c++11 ×3
g++ ×3
templates ×2
visual-c++ ×2
bash ×1
cmake ×1
cmd ×1
constexpr ×1
exit-code ×1
linker ×1
llvm ×1
name-lookup ×1
noexcept ×1
optimization ×1
stl ×1
terminal ×1
type-traits ×1