class MyVect:private std::vector<std::vector<std::string> >
{
typedef std::vector<std::vector<std::string> > super;
public:
///When this is commented out -- No Seg Fault//////////////
std::vector<std::string>& operator[](int plt)
{
return static_cast<super>(*this)[(int)plt];
}
///////////////////////////////////////////////////////////
MyVect()
{
this->resize(4); // this works fine
for (int i = 0; i<4;i++)
{
(*this)[i].resize(3); // I think this doesn't
}
(*this)[0][0] = "hello";
}
};
Run Code Online (Sandbox Code Playgroud)
上面的代码,导致一个seg错误,我无法弄清楚是什么问题?
*** glibc detected *** invalid fastbin entry (free): 0x09267040
Run Code Online (Sandbox Code Playgroud) 我正在构造一个以a std::vector<std::unique_ptr<A> >作为参数的对象。构造函数是这样定义的
class B {
std::vector <std::unique_ptr<A> > e_;
public:
B(std::vector <std::unique_ptr<A> > e) : e_(std::move(e)){}
};
Run Code Online (Sandbox Code Playgroud)
然后用作
std::vector <std::unique_ptr<A> > e;
B b(e);
Run Code Online (Sandbox Code Playgroud)
Xcode出现错误
error: call to implicitly-deleted copy constructor of 'std::__1::unique_ptr<A, std::__1::default_delete<A> >'
:new((void*)__p) _Up(_VSTD::forward<_Args>(__args)...);
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`
Run Code Online (Sandbox Code Playgroud)
为什么即使我正在使用,错误仍然持续存在std::move()?
编辑:如果我使用B b(std::move(e))而不是错误似乎消失B b(e)),有没有办法将move逻辑移至函数的实现?
我有这个代码:
// Example program
#include <iostream>
#include <string>
class Hello{
public:
Hello(){std::cout<<"Hello world!"<<std::endl;}
};
class Base{
public:
Base(const Hello &hello){ this->hello = hello;}
private:
Hello hello;
};
class Derived : public Base{
public:
Derived(const Hello &hello) : Base(hello) {}
};
int main()
{
Hello hello;
Derived d(hello);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
得到的印刷品是:
Hello world!
Hello world!
Run Code Online (Sandbox Code Playgroud)
为什么会这样?
假设我让atomic<int> i;线程 A 使用 memory_order_release 执行原子存储/交换。接下来,线程 B 使用 memory_order_release 执行原子存储。线程 C 执行原子 fetch_add(0, memory_order_acquire);
线程 C 是否从线程A 和 B或仅从线程 B获取依赖项?
是否有直接有效的方式转换std::sub_match为std::basic_string_view(无需构建中间std::basic_string和没有中间堆分配)?或者进一步的抽象级别,是否有替代方法来std::regex_token_iterator迭代表示为std::basic_string_view而不是std::sub_match使用std(C++17)的正则表达式子匹配?
我宁愿使用std::basic_string_viewover的原因std::sub_match是:
std::basic_string_view指的是一个连续连续的类似字符的对象序列,该序列的第一个元素位于零位置。这允许使用charconv's std::from_chars(令人惊讶的是,它没有使用ForwardIterators实现)。对于std::sub_match,情况似乎并非如此,因为它表示为一对BidirectionalIterators。std::basic_string_view 具有更丰富的类似字符串的接口,在某些文件格式的某些特殊情况下促进额外的上下文敏感标记化。我试图在C++的链表中使用overload = operator并编写下面的代码.
template<class T>
class List {
public:
List();
List (T t_data);
List& operator=(const List<T> &L);
private:
template<class L>
class Node {
public:
L data;
Node *next;
Node *prev;
Node(T t_data) {
data = t_data;
next = prev = NULL;
}
};
Node<T> *head;
};
template<class T>
List& List<T>::operator=(const List<T> &L) {
Node<T> *t_head = head;
Node<T> *t_tail = head->prev;
Node<T> *temp;
while(t_head ! = t_tail) {
temp = t_head;
t_head = t_next;
delete temp;
}
head = L.head; …Run Code Online (Sandbox Code Playgroud) 假设我有一个纯粹的抽象基类.类模板实现此接口,并且是专用的.现在,我的问题是这个专业化应该能够处理专业化的子类.所以,我尝试了enable_if,但是子类最终变得抽象......我怎么能绕过这个?
举例:
// This example doesn't work because a subclass of A does not satisfy the
// specialization for B<T>::foo()
class A {
public:
virtual void foo() = 0;
};
template <class T>
class B : public A {
...
public:
...
void foo();
...
};
void B::foo() {
...
}
template <>
void B<A>::foo() {
...
}
class C : A {
...
public:
...
void foo();
...
};
int main() {
B<C> bar; // I was like "this …Run Code Online (Sandbox Code Playgroud) 我正在尝试以下声明:
int (*(*((*foo)(const void *))()))[3];
Run Code Online (Sandbox Code Playgroud)
和
int (*(*(*foo)(const void *)()))[3];
Run Code Online (Sandbox Code Playgroud)
但是编译器给了我一个错误:
error: 'foo' declared as function returning a function
Run Code Online (Sandbox Code Playgroud)
在c ++中完全可以吗?
我尝试在visual studio 2013(社区版)中编译一些代码,但我遇到了问题:以下代码拒绝编译.
struct X
{
X(double y);
};
typedef X Z;
struct Y : public Z
{
using Z::Z;
};
Run Code Online (Sandbox Code Playgroud)
问题是使用Z :: Z的行.它给了我一个错误C2039:'Z':不是'X'的成员
这段代码有效吗?或者这是一个错误?
在我下面的测试用例中,我很困惑为什么析构函数似乎没有被调用,即使我明确地调用它.我注意到只有在模板类型是指针时才会发生这种情况.
代码(内存泄漏,但我试图让最小的例子成为可能)
#include <iostream>
using namespace std;
class A {
public:
A() {}
~A() { cout << "A Destructor"; }
};
template<typename Type>
class TemplateTest {
protected:
Type* start;
Type* end;
Type* iter;
public:
void Generate(unsigned int count) {
start = new Type[count];
end = start + count;
iter = start;
}
void DestroyAll() {
for(; start < end; ++start) {
(*start).~Type();
}
}
void Push(Type val) {
*iter = val;
iter++;
}
};
int main() {
cout << "Non-pointer …Run Code Online (Sandbox Code Playgroud) c++ ×10
c++11 ×5
templates ×3
atomic ×1
atomicity ×1
c++17 ×1
constructor ×1
destructor ×1
enable-if ×1
linked-list ×1
pointers ×1
stdatomic ×1
tokenize ×1
xcode ×1