对于英特尔架构,是否有一种方法可以指示GCC编译器生成的代码总是强制分支预测在我的代码中采用特定方式?英特尔硬件是否支持此功能?那么其他编译器或硬件呢?
我会在C++代码中使用它,我知道我希望快速运行的情况,并且不关心当另一个分支需要被采取时,即使它最近采用了该分支.
for (;;) {
if (normal) { // How to tell compiler to always branch predict true value?
doSomethingNormal();
} else {
exceptionalCase();
}
}
Run Code Online (Sandbox Code Playgroud)
作为Evdzhan Mustafa的后续问题,该提示是否可以在处理器第一次遇到指令时指定一个提示,所有后续的分支预测都能正常运行?
g ++ 4.9.0接受以下代码:
enum E { foo };
struct C {
operator E() const { return foo; }
operator E() { return foo; }
};
int main() {
C c;
switch (c) {
case foo: break;
}
}
Run Code Online (Sandbox Code Playgroud)
但是clang 3.4.1通过以下诊断拒绝它:
12 : error: multiple conversions from switch condition type 'C' to an integral or enumeration type
switch (c)
^ ~
5 : note: conversion to enumeration type 'E'
operator E() const { return foo; }
^
6 : note: conversion …Run Code Online (Sandbox Code Playgroud) c++ switch-statement language-lawyer implicit-conversion c++11
C++ 14中的§5.19/ 3定义了一个整型常量表达式和一个转换后的常量表达式:
一个积分常量表达式是整体的或无作用域枚举类型的表达式,隐式转换为prvalue,其中所述转换后的表达式是一个核心常量表达式.[注意:这些表达式可以用作数组边界(8.3.4,5.3.4),比特字段长度(9.6),如果基础类型不固定(7.2),则作为枚举器初始化器,以及作为比对(7.6). 2).-end note] 转换后的常量类型表达式
T是一个表达式,隐式转换为类型的prvalueT,其中转换后的表达式是核心常量表达式,隐式转换序列仅包含用户定义的转换,左值到右值转换( 4.1),积分促销(4.5)和积分转换(4.7),而不是缩小转换(8.5.4).[注意:这些表达式可以在new表达式(5.3.4)中使用,如case表达式(6.4.2),如果基础类型是固定的(7.2),作为枚举器初始化器,作为数组边界(8.3.4),以及作为整数或枚举非类型模板参数(14.3). - 尾注]
也许我错过了一些东西,但我的第一印象是每个积分常量表达式都是转换后的常量表达式.
编辑
而且我也相信这一段中有错误:
代替:
A converted constant expression of type T is an expression, implicitly converted to a prvalue of type T, ...
它应该是:
A converted constant expression of type T is an expression, implicitly converted to a prvalue of an integral type, ...
此更改允许以下代码进行编译:
#include <iostream>
struct A { operator int() { return …Run Code Online (Sandbox Code Playgroud) 我注意到Clang和GCC之间的行为有以下代码:
class convertible {
public:
operator int() { return 1; }
template <typename T>
operator T() { return 1; }
};
int main () {
convertible x;
switch (x) {} // Clang: OK GCC: Compile error
return 0;
}
Run Code Online (Sandbox Code Playgroud)
GCC回归 error: default type conversion can't deduce template argument for ‘template<class T> convertible::operator T()’
Clang编译代码没有错误和调用 operator int()
我正在使用Clang 6和GCC 8.两者都使用std = c ++ 11
在这种情况下哪个编译器是正确的?
我正在尝试理解一些递归的C++模板代码,我已经交了,而且我遇到了一些奇怪的行为.出于某种原因,编译器似乎能够在编译时添加两个值,但是必须保留左移以用于运行时.即使这样,只有在我尝试使用c ++ 11构建时才会出现问题.
代码(我已经煮浓,以后你会看到)定义2对模板-一个指定的对shft和shft_aux与一个指定的对add和add_aux产生自身递归.顺便说一句,add模板不应该有用,它的唯一目的是证明问题,而不是生成实际min值.
如果我编译此代码没有命令行参数,它编译就好了.但是,如果我指定-std=c++11 -stdlib=libc++,add_aux上的static_assert仍然可以,但shft_aux上的static_assert现在会生成编译时错误static_assert expression is not an integral constant expression.
为什么左移处理不同于添加?
谢谢,克里斯
ps我正在使用clang ++版本 Apple LLVM version 5.1 (clang-503.0.38) (based on LLVM 3.4svn)
#include <climits>
template <unsigned size> struct shft; // forward
template <unsigned size>
struct shft_aux
{
static const int min = shft<size>::min;
};
template <unsigned size>
struct shft
{
typedef shft_aux<size - 1> prev;
static const int min = …Run Code Online (Sandbox Code Playgroud) 以下代码崩溃Microsoft编译器:
class Var
{
public:
template <typename T>
operator T () const
{ }
};
int main()
{
Var v;
switch (v)
{ }
}
Run Code Online (Sandbox Code Playgroud)
我的问题:代码是否正确或编译器是否应该给出适当的错误?是否可以明确地转换为整数类型?