- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我试图强制 Visual C++ 编译器内联一个特定的函数。我知道 inline
或 __forceinline
只是一个建议。根据MSDN ,如果编译器无法内联使用 __forceinline
标记的函数关键字然后它生成警告:
If the compiler cannot inline a function declared with __forceinline, it generates a level 1 warning.
但是我没有收到这样的警告。当我调试我的应用程序时,我看到有一个 call <inlined function address>
在反汇编窗口中,即我的函数没有内联。那么如何检查我的函数是否内联?这是否意味着如果没有生成警告,那么函数被内联并且问题出现在其他地方?
这是我的功能:
__forceinline SecByteBlock aes_generate_iv(size_t blockSize)
{
const int seedSize = 32;
SecByteBlock rngSeed(seedSize);
OS_GenerateRandomBlock(false, rngSeed, rngSeed.size());
RandomPool rngp;
rngp.IncorporateEntropy(rngSeed, rngSeed.size());
SecByteBlock aesIV(blockSize);
rngp.GenerateBlock(aesIV, aesIV.size());
return aesIV;
}
这是 VC++ 无法内联函数的情况(来自 MSDN ):
最佳答案
同一个警告有两个版本:
https://msdn.microsoft.com/en-us/library/aa734011(v=vs.60).aspx
Compiler Warning (level 1) C4714
Visual Studio 6.0
'function' : function marked as __forceinline not inlined
https://msdn.microsoft.com/en-us/library/a98sb923.aspx
Compiler Warning (level 4) C4714
Visual Studio 2015
function 'function' marked as __forceinline not inlined
因此,例如,要在 Visual Studio 2015 中启用警告,您必须使用级别 4 (/W4) 而不是默认情况下的级别 3 (/W3)。
下面是关于如何消除此类编译器行为的一些建议。我已经在 MSVC2015 中解决了这些案例集:
SomeType SomeType::operator++(int)
/SomeType SomeType::operator--(int)
std::string
由 __forceinline 函数中的值返回template <typename T>
__forceinline T int_log2_floor(T v)
{
ASSERT_GT(v, T(0));
if (1 >= v) return 0;
return T((v >= 2 ? int_log2_floor(v / 2) : 0) + 1); // Warning C4714
}
这里的解决方案之一就是展开递归:
template <typename T>
__forceinline T int_log2_floor(T v)
{
ASSERT_GT(v, T(0));
if (1 >= v) return 0;
T ret = 0;
T i = v;
do {
++ret;
i /= 2;
} while (i >= 2);
return ret;
}
class SomeType
{
__forceinline SomeType operator++(int) // Warning C4714
{
const auto it = *this;
m_it++;
return it;
}
//...
iterator m_it
};
这个对我有用:
class SomeType;
SomeType operator++(SomeType & r, int);
class SomeType
{
friend SomeType operator++(SomeType & r, int);
//...
iterator m_it
};
__forceinline SomeType operator++(SomeType & r, int)
{
const auto it = r;
r.m_it++;
return it;
// You still can write here `const auto it = *this; m_it++; return it`,
// but i hell don't know why this works.
}
这个真的很奇怪,但似乎之间有某种干扰__forceinline
-ed 函数介于 inline
-ed 函数之间,因为 std::string
基本上使用第二个 - inline
。
template<typename T>
__forceinline std::string int_to_bin(T i, bool first_bit_is_lowest_bit = false) // Warning C4714
{
std::bitset<sizeof(T) * CHAR_BIT> bs(i);
if (!first_bit_is_lowest_bit) {
return bs.to_string();
}
const std::string bs_str = bs.to_string();
return std::string(bs_str.rbegin(), bs_str.rend());
}
因为warning已经被return-by-value类型触发,这里的解决方法是通过参数返回,通过inline
内联:
template<typename T>
__forceinline void int_to_bin_forceinline(std::string & ret, T i, bool first_bit_is_lowest_bit = false)
{
std::bitset<sizeof(T) * CHAR_BIT> bs(i);
if (!first_bit_is_lowest_bit) {
ret = bs.to_string();
return;
}
const std::string bs_str = bs.to_string();
ret = std::string(bs_str.rbegin(), bs_str.rend());
return;
}
template<typename T>
inline std::string int_to_bin(T i, bool first_bit_is_lowest_bit = false)
{
std::string res;
int_to_bin_forceinline(res, i, first_bit_is_lowest_bit);
return res;
}
不用说,所有 3 个版本都可以通过使用 inline
关键字而不是 __forceinline
来解决。
关于c++ - Visual C++ __forceinline 奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41102421/
我有这种来自 Google map 自动完成的奇怪行为(或者我可能错过了某事)...想法?奇怪的: 您在输入中输入某物,例如“伦敦” 您按 [ENTER] 你按下 [CLEAR] 按钮 你点击进入'输
这段代码与《Learning Java》(Oracle Press Books)一书中的代码完全一样,但它不起作用。我不明白为什么它不起作用,它应该起作用。我用 OpenJDK 和 Sun JDK 7
示例 1 中究竟发生了什么?这是如何解析的? # doesnt split on , [String]::Join(",",("aaaaa,aaaaa,aaaaa,aaaaa,aaaaa,aa
我需要获得方程式系统的解决方案。为此,我使用函数sgesv_()。 一切都很好,它使我感到解决方案的正确结果。 但是我得到一个奇怪的警告。 警告:从不兼容的指针类型传递'sgesv_'的参数3 我正在
我目前在制作动画时遇到一个奇怪的问题: [UIView animateWithDuration:3 delay:0
alert('works'); $(window).load(function () { alert('does not work'); });
我的代码: public class MyTest { public class StringSorter implements Comparator { public
我正在学习 JavaScript。尝试理解代码, function foo (){ var a = b = {name: 'Hai'}; document.write(a.name +''
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
这按预期工作: [dgorur@ted ~]$ env -i env [dgorur@ted ~]$ 这样做: [dgorur@ted ~]$ env -i which date which: no
struct BLA { int size_; int size()const{ return size_; } } int x; BLA b[ 2 ]; BLA * p = &b[
我有以下代码: #test img {vertical-align: middle;} div#test { border: 1px solid green; height: 150px; li
我想大多数使用过 C/C++ 的人都对预处理器的工作原理有一定的直觉(或多或少)。直到今天我也是这么认为的,但事实证明我的直觉是错误的。故事是这样的: 今天我尝试了一些东西,但我无法解释结果。首先考虑
我想为 TnSettings 做 mock,是的,如果通过以下方法编写代码,它就可以工作,问题是我们需要为每个案例编写 mock 代码,如果我们只 mock 一次然后执行多个案例,那么第二个将报告异常
我的项目中有以下两个结构 typedef volatile struct { unsigned char rx_buf[MAX_UART_BUF]; //Input buffer over U
Regex rx = new Regex(@"[+-]"); string[] substrings = rx.Split(expression); expression = "-9a3dcb
我的两个应用程序遇到了一个奇怪的问题。这是设置: 两个 tomcat/java 应用程序,在同一个网络中运行,连接到相同的 MS-SQL-Server。一个应用程序,恰好按顺序位于 DMZ 中可从互联
我目前正在与 Android Api Lvl 8 上的 OnLongClickListener 作斗争。 拿这段代码: this.webView.setOnLongClickListener(new
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
只是遇到了奇怪的事情。我有以下代码: -(void)ImageDownloadCompleat { [self performSelectorOnMainThread:@selector(up
我是一名优秀的程序员,十分优秀!