用字符串参数传递char *报告错误调用构造函数错误

发布于 2025-02-03 20:35:46 字数 853 浏览 2 评论 0原文

template<typename T>
class SharedValue {
public:
    SharedValue(const T& t): valuePtr(new T(t)) {}
    
    const SharedValue& operator = (const T &t) {
        *valuePtr = t;
        return *this;
    }
  
protected:
    dd_shared_ptr<T> valuePtr;
};

typedef SharedValue<std::string> SharedString;
 
int main(int argc, const char * argv[]) {
    // compile succeeded
    SharedString str(argv[0]);

    // compile failed:
    SharedString str2 = argv[0];
   
    return 0;
}

str2施工失败,报告:

 没有可行的转换从'const char *'到'sharedString'(aka'sharedValue&lt; basic_string&lt; char,char_traits&lt; char&gt; char&gt; char&lt; char&lt;
 

为什么str成功了,而str2失败了,有区别吗?

template<typename T>
class SharedValue {
public:
    SharedValue(const T& t): valuePtr(new T(t)) {}
    
    const SharedValue& operator = (const T &t) {
        *valuePtr = t;
        return *this;
    }
  
protected:
    dd_shared_ptr<T> valuePtr;
};

typedef SharedValue<std::string> SharedString;
 
int main(int argc, const char * argv[]) {
    // compile succeeded
    SharedString str(argv[0]);

    // compile failed:
    SharedString str2 = argv[0];
   
    return 0;
}

The str2 construction failed, reporting:

No viable conversion from 'const char *' to 'SharedString' (aka 'SharedValue<basic_string<char, char_traits<char>, allocator<char>>>')

Why str succeeded whereas str2 failed, is there any difference?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑饮青盏花 2025-02-10 20:35:46

为什么str成功而str2失败,有任何区别吗?

是的,两者之间存在区别。特别是,在str中,我们具有直接初始化,而在str2中,我们复制初始化

可以从复制初始化文档指出:

此外, copy-initialization中的隐式转换 必须直接从initializer 中直接产生t,而Eg direct-初始化 期望从初始化器转换为t的构造函数的参数。

(强调我的)

现在,让我们以情况为基础将其应用于给定的示例。

案例1

在这里我们考虑以下陈述:

SharedString str2 = argv[0]; //this is copy initialization

上述复制初始化。并且由于共享string不能直接从右侧的 initializer const char*在右侧产生,因为它需要隐含的转换,该案例1不允许按照上述报价陈述。

情况2

在这里我们考虑以下陈述:

SharedString str(argv[0]); //this is direct initialization

虽然上述使用直接初始化,并且由于有一个隐式转换可用(使用转换构造函数),,这次是根据上述语句允许的。

Why str succeeded whereas str2 failed, is there any difference?

Yes, there is difference between the two. In particular, in str we have have direct initialization while in str2 we have copy initialization.

The behavior of your program can be understood from copy initialization documentation which states:

In addition, the implicit conversion in copy-initialization must produce T directly from the initializer, while, e.g. direct-initialization expects an implicit conversion from the initializer to an argument of T's constructor.

(emphasis mine)

Now let's apply this to your given example on case by case basis.

Case 1

Here we consider the statement:

SharedString str2 = argv[0]; //this is copy initialization

The above uses copy initialization. And as SharedString can't be directly produced from the initializer of type const char* on the right hand side since it requires an implicit conversion, this case 1 isn't allowed in accordance with the above quoted statement.

Case 2

Here we consider the statement:

SharedString str(argv[0]); //this is direct initialization

while the above uses direct initialization and since there is an implicit conversion available(using the converting constructor), this time it is allowed according to the above quoted statement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文