当使用VC12(在Visual Studio 2013 RTM中)[1]编译时,此程序会导致崩溃(在所有构建配置中),而实际上它不应该:
#include <string>
void foo(std::string const& oops = {})
{
}
int main()
{
foo();
}
Run Code Online (Sandbox Code Playgroud)
我知道两个可能有关的无声的错误代码错误:
老实说,我认为这些是不同的.有人知道吗
[1]使用C++ Console Application'向导'创建一个空项目.为简单起见,请禁用预编译标头并保留所有默认值:http://i.stack.imgur.com/rrrnV.png
如果使用VC ++ 2017编译,下面的代码将显示垃圾(或零),如果使用GCC或Clang(https://rextester.com/JEV81255)进行编译,则以下代码将显示“ 1122” 。是VC ++的bug还是我在这里遗漏了一些东西?
#include <iostream>
struct Item {
int id;
int type;
};
int main()
{
auto items = new Item[2]
{
{ 1, 1 },
{ 2, 2 }
};
std::cout << items[0].id << items[0].type;
std::cout << items[1].id << items[1].type;
}
Run Code Online (Sandbox Code Playgroud)
同时,如果元素属于原始类型(例如int),则也可以使用。
c++ new-operator visual-c++ compiler-bug aggregate-initialization
这看起来像是在通用结构上提升为操作数的null的错误.
考虑以下虚拟结构,它覆盖operator==:
struct MyStruct
{
private readonly int _value;
public MyStruct(int val) { this._value = val; }
public override bool Equals(object obj) { return false; }
public override int GetHashCode() { return base.GetHashCode(); }
public static bool operator ==(MyStruct a, MyStruct b) { return false; }
public static bool operator !=(MyStruct a, MyStruct b) { return false; }
}
Run Code Online (Sandbox Code Playgroud)
现在考虑以下表达式:
Expression<Func<MyStruct, MyStruct, bool>> exprA =
(valueA, valueB) => valueA == valueB;
Expression<Func<MyStruct?, MyStruct?, bool>> exprB =
(nullableValueA, nullableValueB) …Run Code Online (Sandbox Code Playgroud) 我string_view为C++ 14项目编写了一个轻量级的包装器,并且在MSVC 2017中它static_assert在编译时触发,但是在运行时相同的代码传递了常规assert.我的问题是,这是一个编译器错误,显示未定义的行为,还是完全不同的东西?
这是蒸馏代码:
#include <cassert> // assert
#include <cstddef> // size_t
class String_View
{
char const* m_data;
std::size_t m_size;
public:
constexpr String_View()
: m_data( nullptr ),
m_size( 0u )
{}
constexpr char const* begin() const noexcept
{ return m_data; }
constexpr char const* end() const noexcept
{ return m_data + m_size; }
};
void static_foo()
{
constexpr String_View sv;
// static_assert( sv.begin() == sv.end() ); // this errors
static_assert( sv.begin() == nullptr ); …Run Code Online (Sandbox Code Playgroud) 我认为这看起来像C#编译器中的一个错误.
考虑这段代码(在方法内):
const long dividend = long.MinValue;
const long divisor = -1L;
Console.WriteLine(dividend % divisor);
Run Code Online (Sandbox Code Playgroud)
它编译时没有错误(或警告).好像是一个bug.运行时,0在控制台上打印.
然后没有const,代码:
long dividend = long.MinValue;
long divisor = -1L;
Console.WriteLine(dividend % divisor);
Run Code Online (Sandbox Code Playgroud)
当它运行时,它正确地导致OverflowException被抛出.
C#语言规范专门提到了这个案例,并说System.OverflowException将抛出一个.它不依赖于上下文checked或unchecked似乎(也是与余数运算符的编译时常量操作数的错误与checked和相同unchecked).
int(System.Int32),而不仅仅是long(System.Int64)发生同样的错误.
相比之下,编译器处理dividend / divisor与const操作数比要好得多dividend % divisor.
我的问题:
我是对的,这是一个错误吗?如果是,它是一个众所周知的错误,他们不希望修复(因为向后兼容性,即使使用% -1编译时常量相当愚蠢-1)?或者我们应该报告它,以便他们可以在即将推出的C#编译器版本中修复它?
标准说
联合的至多一个非静态数据成员可以具有支撑或等于初始化器.
但
struct Point {
Point() {}
Point(int x, int y): x_(x), y_(y) {}
int x_, y_;
};
union U {
int z;
double w;
Point p = Point(1,2);
};
#include <iostream>
int main () {
U u;
std::cout << u.p.x_ << ":" << u.p.y_ << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
打印4196960:0而不是预期的1:2.
我认为这是一个编译器错误.是这样吗?
考虑这个最小的、可重现的例子:
interface Code {
static void main(String[] args) {
symbol(
String.valueOf(
true ? 'a' :
true ? 'b' :
true ? 'c' :
fail()
)
);
}
private static void symbol(String symbol) {
System.out.println(symbol);
}
private static <R> R fail() {
throw null;
}
}
Run Code Online (Sandbox Code Playgroud)
(接近最小,true是一个有用的布尔表达式的替代品。除了第一个之外,我们可以忽略? :(在实际代码中,有很多)。)
这“显然”给出了错误。
4: reference to valueOf is ambiguous
both method valueOf(java.lang.Object) in java.lang.String and method valueOf(char) in java.lang.String match
Run Code Online (Sandbox Code Playgroud)
好的,让我们修复它。这是String.valueOf(Object)我想要的超载 - 我以后可能想添加:
true ? "sss" : …Run Code Online (Sandbox Code Playgroud) 忽略了片刻的荒谬await荷兰国际集团的Enumerable.Range电话.只是在那里引发崩溃行为.它就像一个方法,可以做一些网络IO来构建一个值对象的集合.(的确,这是我看到崩溃发生的地方.)
如果我注释掉该Interlocked.Exchange行,编译器不会崩溃.
public class Launcher
{
private static IEnumerable<int> _foo;
static void Main(string[] args)
{
DoWorkAsync().Wait();
}
private static async Task DoWorkAsync()
{
RefreshCache();
foreach (var element in _foo)
{
Console.WriteLine(element);
}
Console.ReadLine();
}
private static async void RefreshCache()
{
Interlocked.Exchange(ref _foo, await Cache());
}
private static async Task<IEnumerable<int>> Cache()
{
return Enumerable.Range(0, 10);
}
}
Run Code Online (Sandbox Code Playgroud)
更改RefreshCache()为此可防止编译器崩溃:
private static async void RefreshCache()
{
var foo = await Cache();
Interlocked.Exchange(ref _foo, foo);
} …Run Code Online (Sandbox Code Playgroud) 考虑这个程序:
#include <iostream>
template<bool Debug = false, int Line = __LINE__>
constexpr int adds(const int& a, const int& b) {
if (Debug)
std::cout << __FUNCTION__ << " called on line " << Line << '\n';
return (a + b);
}
int main() {
std::cout << adds(3, 7) << '\n';
std::cout << adds<true, __LINE__> (5, 9) << '\n';
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我尝试在DebugVisual Studio 2017 模式下编译和构建它时,会生成这些编译器错误:
1>------ Build started: Project: Simulator, Configuration: Debug x64 ------
1>main2.cpp
1>c:\***\main2.cpp(12): error C2672: 'adds': …Run Code Online (Sandbox Code Playgroud) c++ compiler-errors visual-c++ compiler-bug visual-studio-2017
#include <memory>
struct Base
{
Base() = default;
Base(Base const&) = delete;
Base(Base&&) = default;
};
struct Derived : Base
{
Derived() = default;
Derived(Derived const&) = delete;
Derived(Derived&&) = default;
};
auto foo()
-> Base
{
Derived d;
return d; // ERROR HERE
}
Run Code Online (Sandbox Code Playgroud)
导致以下错误:
prog.cc: In function 'Base foo()': prog.cc:21:12: error: use of deleted function 'Base::Base(const Base&)'
return d;
^
Run Code Online (Sandbox Code Playgroud)
根据[class.copy]/32:
当满足复制/移动操作的省略标准时,但不满足异常声明,并且要复制的对象由左值指定,或者当返回语句中的表达式是(可能带有括号的)id-时表达式,用于在最内层封闭函数或lambda-expression的body或parameter-declaration-clause中声明的具有自动存储持续时间的对象,首先执行重载决策以选择复制的构造函数,就像对象由rvalue指定一样
如果上面的句子意味着被解析为(copy elision criteria met && lvalue) …
compiler-bug ×10
c++ ×6
c# ×3
visual-c++ ×3
async-await ×1
c++11 ×1
c++14 ×1
casting ×1
g++ ×1
generics ×1
interlocked ×1
java ×1
modulus ×1
new-operator ×1
nullable ×1
operators ×1
return ×1