- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
Justin's answer在另一个问题上做了一个我觉得很有趣但无法解释的观察。考虑以下代码:
std::vector<std::string> v;
v.push_back("Hello, world!"); // Doesn't call strlen.
v.emplace_back("Hello, world!"); // Calls strlen.
如果您查看程序集, emplace_back
generates a call to strlen
, 而 push_back
does not (使用 -Ofast
在 gcc 8.1 和 clang 6.0 中测试)。
为什么会这样?为什么不能emplace_back
优化 strlen
在这里打电话?我最初的想法是 push_back
正在隐式创建 std::string
在 函数调用之前(因此 std::string
构造函数直接传递字符串文字,这是最佳处理方式),而 emplace_back
创建 std::string
在 函数调用之后(因此 std::string
构造函数被转发 字符串文字,我认为它已经从 const char [N]
衰减到 const char *
,因此需要一个strlen
电话)。
但是 emplace_back
takes a T&&
parameter , 和 my tests show that the string literal shouldn't be decaying to a pointer here .显然我忽略了一些东西。
最佳答案
strlen
调用在慢路径的外联函数体中;该函数体必须对 const char (&)[42]
类型的所有参数有效(在您的 godbolt 示例中),包括并非源自 41 个字符的字符串文字且没有嵌入空值的参数.
快速路径内联到 foo
中,它会在编译时计算长度。
关于c++ - 为什么 emplace_back ("Hello") 调用 strlen?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51219755/
我正在尝试创建执行搜索的线程 vector 。这是我的 SearchThread 类中的重要内容。 class SearchThread { explicit SearchThread
给定以下代码 测试.h: #ifndef __graph_aufbau_header__ #define __graph_aufbau_header__ #include #include #in
关于 someFunctionOne 和 someFunctionTwo。 当每个都放置到 map 中时,最终结果是否仍然相同? 指针是否升级为智能指针? struct MyStruct {
我对 emplace_back 的行为有点困惑。一起来看看吧。 struct innerObject { innerObject(int ); }; class outterObject {
我尝试将类 cBar 的两个实例放置到具有 emplace_back 函数的 vector 中。 根据reference调用 emplace_back 仅保留 vector 中的位置,然后“就地”创建
我想知道是否可以使用 emplace_back 将项目存储到 vector 中,emplace_back 是一种派生自 vector 所期望的类的类型。 例如: struct fruit {
这是我日常工作中的一段代码。我只想问你这两种情况有什么区别,尤其是在性能方面。 std::vector > aVec; // case 1 aVec.emplace_back("hello", "bo
我向你展示我的问题 我有 2 个列表,将它们命名为 A 和 B。 list > A = {{1},{2},{3}}; list > B = {{4},{5},{6}}; 我想要的是 A = {{1,4
#include #include using namespace std; class test{ public: test(){
似乎添加默认构造函数会阻止调用 emplace_back 并产生错误消息:“静态断言失败:类型不可分配”(gcc 5.3 with -std=c++14)。这是一个说明问题的简单代码: class A
我编写了一个简单的程序来尝试在标准库容器中就地创建对象。这是我写的: #include #include class AB { public: explicit AB(int n);
我想知道我是否正确理解emplace_back #include using namespace std; struct Hero { Hero(const string&) {}
这个问题在这里已经有了答案: Emplace an aggregate in std::vector (3 个答案) 关闭 4 年前。 为什么我可以直接调用构造函数,但是当我在 emplace_ba
我正在尝试为我的用户定义结构使用 emplace_back: #include #include #include struct IDNumber { IDNumber(std::vec
考虑下面的例子: #include #include class S { public: S() { puts("S()"); } S(int) { puts("S(int)"); }
我正在创建一个指向新对象的新指针,并立即将 push_front 放入双端队列。我想改为使用 emplace_front,但遇到编译器错误。 我的对象构造函数需要 1 个字符串参数。 std::deq
所以我一直在制作一个对象池类,它是这样使用的: class MagicTrick { public: MagicTrick(int magic) : _magic(magic) {}
#include #include struct T{ T(){ std::cout vec; vec.push_back(T()); vec.push_
我在实现我自己的模板类时遇到了困难,我想在其中添加我自己的 emplace_back 函数实现。由于我仍在学习模板设计,非常感谢您在这里提供意见。 template class MydataStru
这个问题在这里已经有了答案: emplace_back() does not behave as expected (2 个答案) 关闭 8 年前。 我很好奇这段代码的输出结果。 #include
我是一名优秀的程序员,十分优秀!