首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >clang++ 4.2中的“所选构造函数在复制初始化错误中是显式的”

clang++ 4.2中的“所选构造函数在复制初始化错误中是显式的”
EN

Stack Overflow用户
提问于 2013-06-24 02:44:49
回答 1查看 11.3K关注 0票数 9

我有clang++ 4.2

代码语言:javascript
复制
Apple LLVM version 4.2 (clang-425.0.28) (based on LLVM 3.2svn)
Target: x86_64-apple-darwin11.4.2
Thread model: posix

当我尝试编译这段c++11代码时:

代码语言:javascript
复制
class ContextSummary {
    int id;
    int hops;
    std::map<std::string, int> db {};
    std::time_t timestamp;

ContextSummary(int id, const std::map<std::string, int>& db = {}, int hops = 3, std::time_t timestamp = 0)
{
    this->id = id;
    this->db = db;
    this->hops = hops;
    this->timestamp = timestamp;
}

我收到了这个错误消息。代码可以很好地与g++4.8配合使用

代码语言:javascript
复制
error: 
      chosen constructor is explicit in copy-initialization
  ...id, const std::map<std::string, int>& db = {}, int hops = 3, std::time_t...

                                           ^    ~~

这是clang++错误吗?如何绕过此错误?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-06-24 03:32:49

复制我在评论中说的话

,这是http://cplusplus.github.io/LWG/lwg-active.html#2193。我不确定“提议的决议”或类似的东西是否会出现在C++14中。在复制初始化上下文中使用显式默认构造函数的值初始化是否格式良好本身也是一个核心语言DR http://open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1518,它解释了可能的跨编译器差异。

如果您的实现实现了http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1494,并且以下是默认参数的有效语法,那么您可以这样做

代码语言:javascript
复制
const std::map<std::string, int>& db{}

不幸的是,这是不允许的(我认为原因可能是显式地传递参数,你也不能直接初始化参数,所以为什么允许它作为默认参数呢?)所以在我看来,唯一的方法就是显式地创建它

代码语言:javascript
复制
const std::map<std::string, int>& db = std::map<std::string, int>{}

决定你是否想要以可能的更多代码为代价摆脱冗余。一些替代方案

代码语言:javascript
复制
const std::map<std::string, int>& db = decltype(ContextSummary::db){}
const std::map<std::string, int>& db_ = decltype(db){}
const std::map<std::string, int>& db = std::decay<decltype(db)>::type{}
票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17264067

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档