小编und*_*e_d的帖子

如何转发在命名空间std中声明模板类?

#ifndef __TEST__
#define __TEST__

namespace std
{
    template<typename T>
    class list;
}

template<typename T>
void Pop(std::list<T> * l)
{
    while(!l->empty())
        l->pop();
}

#endif
Run Code Online (Sandbox Code Playgroud)

并在我的主要使用该功能.我收到错误.当然,我知道有更多的模板参数std::list(我认为是分配器).但是,这是不重要的.我是否必须知道模板类的完整模板声明才能转发声明它?

编辑:我之前没有使用指针 - 这是一个参考.我会用指针试一试.

c++ templates g++

129
推荐指数
3
解决办法
13万
查看次数

为什么(仅)某些编译器对相同的字符串文字使用相同的地址?

https://godbolt.org/z/cyBiWY

我可以'some'在MSVC生成的汇编代码中看到两个文字,但只有一个有clang和gcc.这导致完全不同的代码执行结果.

static const char *A = "some";
static const char *B = "some";

void f() {
    if (A == B) {
        throw "Hello, string merging!";
    }
}
Run Code Online (Sandbox Code Playgroud)

任何人都可以解释这些编译输出之间的差异和相似之处吗?为什么即使没有请求优化,clang/gcc也会优化某些内容?这是某种未定义的行为吗?

我还注意到,如果我将声明更改为下面显示的声明,则clang/gcc/msvc根本不会"some"在汇编代码中留下任何声明.为什么行为不同?

static const char A[] = "some";
static const char B[] = "some";
Run Code Online (Sandbox Code Playgroud)

c++ string-literals string-interning language-lawyer

88
推荐指数
3
解决办法
6562
查看次数

为什么C++不允许使用匿名结构?

一些C++编译器允许匿名联合和结构作为标准C++的扩展.这有点语法糖,偶尔会非常有帮助.

阻止它成为标准一部分的理由是什么?是否存在技术障碍?一个哲学的?或者仅仅是不足以证明它的合理性?

这是我正在谈论的样本:

struct vector3 {
  union {
    struct {
      float x;
      float y;
      float z;
    };
    float v[3];
  };
};
Run Code Online (Sandbox Code Playgroud)

我的编译器会接受这个,但它警告"无名结构/联合"是C++的非标准扩展.

c++ struct unions

87
推荐指数
3
解决办法
5万
查看次数

error :: make_unique不是'std'的成员

我正在尝试编译代码审查上发布的以下线程池程序来测试它.

https://codereview.stackexchange.com/questions/55100/platform-independant-thread-pool-v4

但我得到了错误

threadpool.hpp: In member function ‘std::future<decltype (task((forward<Args>)(args)...))> threadpool::enqueue_task(Func&&, Args&& ...)’:
threadpool.hpp:94:28: error: ‘make_unique’ was not declared in this scope
     auto package_ptr = make_unique<task_package_impl<R, decltype(bound_task)>>  (std::move(bound_task), std::move(promise));
                        ^
threadpool.hpp:94:81: error: expected primary-expression before ‘>’ token
     auto package_ptr = make_unique<task_package_impl<R, decltype(bound_task)>>(std::move(bound_task), std::move(promise));
                                                                             ^
main.cpp: In function ‘int main()’:
main.cpp:9:17: error: ‘make_unique’ is not a member of ‘std’
 auto ptr1 = std::make_unique<unsigned>();
             ^
main.cpp:9:34: error: expected primary-expression before ‘unsigned’
 auto ptr1 = std::make_unique<unsigned>();
                              ^
main.cpp:14:17: error: ‘make_unique’ is not a member …
Run Code Online (Sandbox Code Playgroud)

c++ compiler-errors unique-ptr c++11 c++14

77
推荐指数
3
解决办法
8万
查看次数

Qt中的C++信号到QML插槽

我想从C++向我的QML文件中的Slot发送一个Signal.我已经让它没有和原始类型参数工作,但如果我想发送QString到我的QML插槽我连接时出错.

我在main.cpp中连接

QObject *contentView = rootObject->findChild<QObject*>(QString("contentView"));
QObject::connect(&myObj,      SIGNAL(finishedGatheringDataForItem(QString)), 
                 contentView, SLOT(updateViewWithItem(QString)));
Run Code Online (Sandbox Code Playgroud)

我的qml文件的相关部分

Rectangle {
        objectName: "contentView"
        function updateViewWithItem(string) { console.log('got some Items'); }  // slot
}
Run Code Online (Sandbox Code Playgroud)

错误:

Object::connect: No such slot QDeclarativeRectangle_QML_2::updateViewWithItem(QString)
Run Code Online (Sandbox Code Playgroud)

c++ qt signals-slots qml

51
推荐指数
2
解决办法
7万
查看次数

C++如何处理&&?(短路评估)

当遇到(bool1 && bool2)时,如果发现bool1为false,c ++是否会尝试检查bool2,或者它是否像PHP那样忽略它?

对不起,如果它是一个问题的基础,但我真的无法在Schildt或互联网上找到这一点.

c++ short-circuiting and-operator logical-and

45
推荐指数
3
解决办法
3万
查看次数

空的初始化列表是否为有效的C代码?

通常用于{0}初始化a struct或a,array但考虑第一个字段不是标量类型时的情况.如果第一个字段struct Person是另一个struct或数组,则该行将导致错误(error: missing braces around initializer).

struct Person person = {0};
Run Code Online (Sandbox Code Playgroud)

至少GCC允许我使用空的初始化列表来完成同样的事情

struct Person person = {};
Run Code Online (Sandbox Code Playgroud)

但这是有效的C代码吗?

另外:这条线是否保证给出相同的行为,即零初始化struct

struct Person person;
Run Code Online (Sandbox Code Playgroud)

c standards gcc struct initializer

35
推荐指数
3
解决办法
1万
查看次数

std :: string是否保证不会自发回馈内存?

是否由标准保证,std::string如果从较小的字符串重新分配,则不会自发地回放分配的内存?

换一种说法:

std::string str = "Some quite long string, which needs a lot of memory";
str = "";
str = "A new quite long but smaller string"; // Guaranteed to not result in a heap allocation?
Run Code Online (Sandbox Code Playgroud)

我问,因为我依赖于此以避免堆碎片.

c++ heap-memory stdstring language-lawyer

31
推荐指数
2
解决办法
2881
查看次数

在SQL Server数据库中重建索引的频率应该是多少?

目前,我们的数据库大小为10 GB,每月增长约3 GB.我经常听说应该不时重建索引,以改善查询执行时间.那么我应该多久重建给定场景中的索引?

sql-server indexing sql-server-2005 maintenance-plan

27
推荐指数
2
解决办法
3万
查看次数

如何迭代向量并知道元素的索引?

我需要访问向量中的每个元素,并且还知道元素所在的索引.

到目前为止,我可以提出两种方法

 for (iterator it= aVector.begin(), int index= 0; it!= aVector.end(); ++it, ++index)
Run Code Online (Sandbox Code Playgroud)

留下类型签名.它看起来我不能使用汽车

 for (int index = 0; index < aVector.size(); ++index)
{
    // access using []
}
Run Code Online (Sandbox Code Playgroud)

哪一个更有效率还是有更好的方法来做到这一点?

c++ iteration algorithm stl vector

26
推荐指数
3
解决办法
2万
查看次数