以下代码使用GCC 8.2编译,但不与Clang 6.0.1编译:
// A struct named Foo.
struct Foo
{
// Data member of type 'int'.
int val;
// Default constructor (constexpr).
constexpr Foo() noexcept : val(0) {}
};
// A struct named Bar.
struct Bar : Foo
{
// Make use of the constructors declared in Foo.
using Foo::Foo;
// A constructor taking an object of type Foo.
// COMMENTING THIS CONSTRUCTOR SOLVE THE COMPILATION ISSUE.
constexpr Bar(Foo const obj) noexcept : Foo(obj) {}
};
// A struct …Run Code Online (Sandbox Code Playgroud) 这只是一个快速的问题,可以正确理解当您使用如下构造函数创建类时会发生什么:
class A
{
public:
A() {}
};
Run Code Online (Sandbox Code Playgroud)
我知道没有生成默认构造函数,因为它已经定义,但是由编译器生成的复制和赋值构造函数,或者换句话说,我是否需要声明私有复制构造函数和私有赋值运算符以防止这种情况发生?
class A
{
private:
// needed to prevent automatic generation?
A( const A& );
A& operator=( const A& );
public:
A() {}
};
Run Code Online (Sandbox Code Playgroud) c++ copy-constructor default-constructor assignment-operator
我已将问题减少到以下示例代码:
class pokemon{
public:
pokemon(int n);
};
class MewTwo : public pokemon {
public:
MewTwo(int n);
};
MewTwo::MewTwo(int n) {}
Run Code Online (Sandbox Code Playgroud)
这会产生错误:
没有匹配函数来调用'pokemon :: pokemon()'
我认为正在发生的是当我尝试编写MewTwo构造函数时,调用pokemon的默认构造函数,该构造函数不存在.我对C++比较陌生,所以我只想猜测一下.有任何想法吗?
约束:修复不能修改或添加公共成员到类.
在C++ 11中,有两个版本std::vector::resize():
void resize( size_type count );
void resize( size_type count, const value_type& value);
Run Code Online (Sandbox Code Playgroud)
我理解(正如对该问题的答案之一的评论之一所建议的),第一个要求value_type是默认可构造的,而第二个要求它是可复制构造的.但是,(gcc 4.7.0)
using namespace std;
typedef int block[4];
vector<block> A;
static_assert(is_default_constructible<block>::value,";-("); // does not fire
A.resize(100); // compiler error
Run Code Online (Sandbox Code Playgroud)
所以要么我的理解是错误的,要么gcc是错误的.哪一个?
boost::optional< T >如果基础类型T是非默认的可构造的,不可复制/可移动的,但我应该做什么来初始化,但是一个实例仍然可以存在?
是否boost::optional由于任何语义原因禁止使用某些成员函数template< typename... Args > boost::optional< T >::construct(Args && ...args),它将所有参数传递给就地operator new完全构造对象(对于非ref类型T)?Variant是具有非成员函数的std::make_shared< T >.
在我看来,我的问题可以通过使用std::unique_ptr/ 解决std::shared_ptr,但在这种情况下我的问题是:"为什么boost::optional进展被冻结?".
为什么
struct wrapper
{
explicit wrapper(void *);
wrapper() = default;
int v;
};
int main() { return wrapper().v; } // You should run this in Debug mode
Run Code Online (Sandbox Code Playgroud)
返回0xCCCCCCCC,而
struct wrapper { wrapper() = default; int v; };
int main() { return wrapper().v; }
Run Code Online (Sandbox Code Playgroud)
和
struct wrapper { int v; };
int main() { return wrapper().v; }
Run Code Online (Sandbox Code Playgroud)
都回归0?
c++ constructor default-constructor visual-c++ visual-c++-2013
在你开始将其标记为重复之前,我已经读过这个 .但它没有回答我的问题.链接的问题谈到了C++ 98和C++ 03,但我的问题是关于C++ 11引入的默认构造函数.
考虑以下程序(请参阅此处的实时演示):
#include <iostream>
struct Test
{
int s;
float m;
Test(int a,float b) : s(a),m(b)
{ }
Test()=default;
}t;
int main()
{
std::cout<<t.s<<'\n';
std::cout<<t.m<<'\n';
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,编译器提供的默认构造函数总是初始化内置类型,在C++ 11和C++ 14中默认为0,当它们是class&struct成员时.这种行为是否由C++ 11标准保证?
c++ initialization default-constructor language-lawyer c++11
应该在以下代码中调用哪个构造函数,为什么?
struct S
{
int i;
S() = default;
S(void *) : i{1} { ; }
};
S s{{}};
Run Code Online (Sandbox Code Playgroud)
如果我使用clang(来自主干),则调用第二个.
如果第二个构造函数被注释掉,那么S{{}}仍然是有效的表达式,但是(我相信)S{}在这种情况下调用默认构造的实例中的move-constructor .
为什么转换构造函数在第一种情况下优先于默认值?
构造函数的这种组合的目的S是保存其std::is_trivially_default_constructible_v< S >属性,除了有限的一组情况,当它应该以某种方式初始化时.
c++ initialization default-constructor language-lawyer c++14
我正在为一些没有编译的代码做一些测试,我发现这段代码:
struct A {
A(int) {};
virtual void foo() = 0;
};
struct B : public virtual A {
virtual void bar() = 0;
};
struct C : public B {
C() : A(1) {}
virtual void foo() override {}
virtual void bar() override {}
};
int main() {
C c;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在C++ 11中无法编译(在g ++ 7.0.1上)‘B::B()’ is implicitly deleted because the default definition would be ill-formed,而在C++ 14中编译成功.
我试图找出C++ 14的哪个新功能允许它工作,但无济于事.cppreference中的描述没有提到任何类似的东西.
为什么这段代码可以用C++ 14编译而不能用C++ 11编译?
考虑以下小程序:
#include <vector>
class A {
int a;
int b;
public:
explicit A() = default;
A(int _a, int _b) : a(_a), b(_b) {}
int f(const A& a) { return 0; }
int f(std::vector<int> a) { return 1; }
};
int g(const A& a) { return 2; }
int g(std::vector<int> a) { return 3; }
int main() {
A a(1,2);
// a.f({}); //ambiguous according to gcc
g({}); //ambiguous according to gcc
return 0;
}
Run Code Online (Sandbox Code Playgroud)
GCC 10.2 拒绝编译它:它说调用g({})和a.f({})是模棱两可的。Clang …
c++ ×9
c++11 ×3
c++14 ×2
boost ×1
clang++ ×1
constexpr ×1
constructor ×1
inheritance ×1
noncopyable ×1
stdvector ×1
visual-c++ ×1