我有简单的代码与转换运算符,似乎所有编译器给出不同的结果,好奇哪个编译器,如果有的话,是正确的?我尝试了不同的组合,但下面的组合是最有趣的.代码是使用C++ 11标志编译的,但在C++ 03中也可能会出现相同的行为.
#include <iostream>
struct call_operator {
template<typename T>
operator T() {
std::cout << __FUNCTION__ << std::endl;
return {};
}
template<typename T>
operator const T&() const {
std::cout << __FUNCTION__ << std::endl;
static T t;
return t;
}
template<typename T>
operator T&() const {
std::cout << __FUNCTION__ << std::endl;
static T t;
return t;
}
};
int main() {
(void)static_cast<int>(call_operator());
(void)static_cast<const int&>(call_operator());
(void)static_cast<int&>(call_operator());
}
Run Code Online (Sandbox Code Playgroud)
铛 - 3.6:
operator int
operator const int &
operator int &
Run Code Online (Sandbox Code Playgroud)
克++ - 4.9: …
我有一个我正在测试的简单示例,我注意到当涉及operator new时,gcc优化(-O3)似乎不如clang那样好.我想知道可能是什么问题,是否有可能迫使gcc以某种方式生成更优化的代码?
template<typename T>
T* create() { return new T(); }
int main() {
auto result = 0;
for (auto i = 0; i < 1000000; ++i) {
result += (create<int>() != nullptr);
}
return result;
}
#clang3.6++ -O3 -s --std=c++11 test.cpp
#size a.out
text data bss dec hex filename
1324 616 8 1948 79c a.out
#time ./a.out
real 0m0.002s
user 0m0.001s
sys 0m0.000s
#gcc4.9 -O3 -s --std=c++11 test.cpp
#size a.out
text data bss dec hex filename
1484 …Run Code Online (Sandbox Code Playgroud) 如果初始化语法和'constexpr if'中有以下内容,我找不到有关新C++ 17的任何信息:
http://open-std.org/JTC1/SC22/WG21/docs/papers/2016/p0128r1.html
然而,Clang-HEAD支持语法...
constexpr auto f() { return true; }
int main() {
if constexpr(constexpr auto x = f(); x) { }
}
Run Code Online (Sandbox Code Playgroud)
在线代码 - > http://melpon.org/wandbox/permlink/dj3a9ChvjhlNc8nr
是否constexpr if有标准保证的初始化程序,因为constexpr if它只是一个" if有constexpr"或者它不能保证并且必须明确地添加到标准中?