RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 595871
Accepted
Harry
Harry
Asked:2020-11-25 22:48:02 +0000 UTC2020-11-25 22:48:02 +0000 UTC 2020-11-25 22:48:02 +0000 UTC

int 的 emplace_back

  • 772

‒ 你知道发生了什么事吗?
‒ 您想解释一下吗?
‒ 我可以自己解释......你明白吗?

对不起题词,但是......我有一种我可以解释的感觉,但我不明白(反之亦然......)这里发生了什么:

std::vector<int> v;
v.emplace_back(1);
v.emplace_back({1});
v.emplace_back(int{1});

标准是如何解释这三个表达式的,为什么会有这样奇怪的诊断

emplace_back:函数不接受 1 个参数

在中线?

c++
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    Vlad from Moscow
    2020-11-25T22:55:15Z2020-11-25T22:55:15Z

    对于这个提案

    v.emplace_back({1});
    

    问题是当使用模板参数时,模板参数的类型不是从花括号参数中推断出来的。并且该函数emplace_back使用模板参数。

    来自 C++ 标准(14.8.2.1 Deducing template arguments from a function call)

    1 模板参数推导是通过将每个函数模板参数类型(称为 P)与调用的相应参数类型(称为 A)进行比较来完成的,如下所述。如果从 P 中移除引用和 cv 限定符给出某些 P0 的 std::initializer_list 并且参数是初始化列表 (8.5.4),则对初始化列表的每个元素执行推导,将 P0 作为函数模板参数类型和初始化元素作为其参数。否则,初始化列表参数会导致参数被视为非推导上下文

    您可以使用以下类声明示例模拟相同的错误

    #include <iostream>
    #include <initializer_list>
    
    struct A
    {
        template <typename ...T>
        void f( T &&... ) const {}
    };
    
    int main() 
    {
        A().f( { 1 } );
    
        return 0;
    }
    

    编译器可能会发出以下诊断消息

    prog.cpp: In function 'int main()':
    prog.cpp:13:15: error: no matching function for call to 'A::f(<brace-enclosed initializer list>)'
      A().f( { 1 } );
                   ^
    prog.cpp:8:7: note: candidate: void A::f(T&& ...) const [with T = {}]
      void f( T &&... ) const {}
           ^
    prog.cpp:8:7: note:   candidate expects 0 arguments, 1 provided
    

    如果emplace_back用非模板方法替换push_back,则相应的调用将编译,因为函数参数的类型从声明的实例化向量的类型中已知,并且是类型int。

    因此,您将明确需要指定函数模板参数的类型

    例如,

    #include <iostream>
    #include <initializer_list>
    
    struct A
    {
        template <typename ...T>
        void f( T &&... ) const {}
    };
    
    int main() 
    {
        A().f<int>( { 1 } );
    
        return 0;
    }
    

    或者为了emplace_back

    #include <iostream>
    #include <vector>
    #include <initializer_list>
    
    int main() 
    {
        std::vector<int> v;
    
        v.emplace_back<int>( { 1 } );
    
        for ( int x : v ) std::cout << x << ' ';
        std::cout << std::endl;
    
        return 0;
    }
    

    将程序输出到控制台

    1
    

    或@alexolut在评论中建议的示例

    #include <iostream>
    #include <vector>
    
    struct S 
    {
        S(std::initializer_list<int>) {}
    };
    
    int main() 
    {
        std::vector<S> v;
        v.emplace_back<std::initializer_list<int>>({1});
    }
    

    关于给定调用中的表达式

    v.emplace_back(int{1});
    

    那么这里使用显式类型转换,即所谓的函数式表示法。

    来自 C++ 标准(5.2.3 显式类型转换(函数符号))

    3 因此,简单类型说明符或类型名称说明符后跟花括号初始化列表创建指定类型直接列表初始化 (8.5.4) 的临时对象,具有指定的花括号初始化列表,及其value 是作为 prvalue 的临时对象。

    • 10

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    如何停止编写糟糕的代码?

    • 3 个回答
  • Marko Smith

    onCreateView 方法重构

    • 1 个回答
  • Marko Smith

    通用还是非通用

    • 2 个回答
  • Marko Smith

    如何访问 jQuery 中的列

    • 1 个回答
  • Marko Smith

    *.tga 文件的组重命名(3620 个)

    • 1 个回答
  • Marko Smith

    内存分配列表C#

    • 1 个回答
  • Marko Smith

    常规赛适度贪婪

    • 1 个回答
  • Marko Smith

    如何制作自己的自动完成/自动更正?

    • 1 个回答
  • Marko Smith

    选择斐波那契数列

    • 2 个回答
  • Marko Smith

    所有 API 版本中的通用权限代码

    • 2 个回答
  • Martin Hope
    jfs *(星号)和 ** 双星号在 Python 中是什么意思? 2020-11-23 05:07:40 +0000 UTC
  • Martin Hope
    hwak 哪个孩子调用了父母的静态方法?还是不可能完成的任务? 2020-11-18 16:30:55 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    user207618 Codegolf——组合选择算法的实现 2020-10-23 18:46:29 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    Arch ArrayList 与 LinkedList 的区别? 2020-09-20 02:42:49 +0000 UTC
  • Martin Hope
    iluxa1810 哪个更正确使用:if () 或 try-catch? 2020-08-23 18:56:13 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5