Geçtiğimiz günlerde Why does a const object requires a user-provided default constructor?, bir Why does C++ require a user-provided default constructor to default-construct a const object? kopyası olarak işaretlendi. Bu davranışın derleyicinin daha yeni sürümlerinde sunulup sunulmadığını görmek için gcc (g ++ - 4.7, g ++ - 4.8, g ++ - 4.9) ve clang (3.4 ve 3.5) sürümlerini test etmek için coliru ve rextexter kullanıyorum . gcc'ye expectedly ancakNeden gcc, kullanıcı tarafından bildirilen varsayılan yapıcı olmadan bir const nesnesine izin verir, ancak clang değil?
error: default initialization of an object of const type 'const A' requires a user-provided default constructor
A const a;
^
ve MSVC ne yapar: ile
struct B{
B():x(42){}
int doSomeStuff() const{return x;}
int x;
};
struct A{
A(){}//other than "because the standard says so", why is this line required?
B b;//not required for this example, just to illustrate
//how this situation isn't totally useless
};
int main(){
const A a;
}
clang hataları dışarı:
class A {
public:
void f() {}
};
int main()
{
A a; // OK
const A b; // ERROR
a.f();
return 0;
}
ve: Burada sırasıyla her iki sorunun çekilmiş iki test durumları var.
§ 8,5
6 To default-initialize an object of type T means:
— if T is a (possibly cv-qualified) class type (Clause 9), the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);
[...]
If a program calls for the default initialization of an object of a const-qualified type T, T shall be a class type with a user-provided default constructor.
11 If no initializer is specified for an object, the object is default-initialized; [...]
ikinci soruya mevcut olmayan POD dil belkide ben kaçırıyorum n3337 eksik gibi görünüyor: açıkça standart tırnak demek çünkü ben deliriyorum olabilir belki düşündü Değişmiş olabilecek bir şey. Bu bir hata mı, kopya mı, yoksa bir şey mi eksik?
Evet, gerçekten de bir gcc en böcek. –
Bu hata, [DR 253] 'e (http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#253) karşılık gelir; bu da GCC'nin davranışının amaçlanan davranış ve standart olduğunu gösterir. tam olarak bunu gerektirecek şekilde güncellenecektir. – hvd
@hvd Ah, teşekkürler. DR 253'ün bahsini gördüm ama asla izlemedim. Cevabımı güncelleme. – bames53