c++ 17 ny_cast;失败隐式CTOR,抛出异常

发布于 2025-02-11 08:39:40 字数 878 浏览 1 评论 0原文

我的我的带有一个隐式ctor 我的(int i),我希望将int投入到我的身上,所以我有:

#include <any>
#include <iostream>
using namespace std;
struct My {
    My(int i) { cout << "int ctor\n"; }
};
ostream& operator << (ostream& os, const My& m) {
    os << "My object\n";
    return os;
}
int main() {
    any a = 1;
    auto am = My(any_cast<int>(a));
    cout << a.type().name() << ": " << any_cast<int>(a) << '\n';
    cout << a.type().name() << ": " << any_cast<My>(a) << '\n';
    return 0;
}

运行后,它打印出来:

int ctor
i: 1
i: terminate called after throwing an instance of 'std::bad_any_cast'
  what():  bad any_cast

我期望那个any_cast&lt; my&gt;(a)应该能够调用我的(a)构造一个新对象,但实际上不是。

我有什么误解吗?

I've having My with an implicit ctor My(int i), I wish to cast an int into My, so I have:

#include <any>
#include <iostream>
using namespace std;
struct My {
    My(int i) { cout << "int ctor\n"; }
};
ostream& operator << (ostream& os, const My& m) {
    os << "My object\n";
    return os;
}
int main() {
    any a = 1;
    auto am = My(any_cast<int>(a));
    cout << a.type().name() << ": " << any_cast<int>(a) << '\n';
    cout << a.type().name() << ": " << any_cast<My>(a) << '\n';
    return 0;
}

Upon running, it prints:

int ctor
i: 1
i: terminate called after throwing an instance of 'std::bad_any_cast'
  what():  bad any_cast

I expected that any_cast<My>(a) should be able to call My(a) to construct a new object, but actually not.

Did I mis-undertand anything?

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

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

发布评论

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

评论(3

攒眉千度 2025-02-18 08:39:40

这根本不是std :: Any_cast工作的方式。从字面上看,它不是Eg static_cast IS之类的类型之间的铸件。

您必须必须提供存储在std ::任何对象中的确切类型以访问其值(通过std :: Any_cast)。不可能暗示任何转换。

因为仅在运行时确定std ::任何中存储的类型允许转换。这显然是不可行的或不可能的。

std ::任何是工作的正确工具。特别是它将不允许您规避C ++的静态性质。

This is simply not how std::any_cast works. It is not literally a cast between types like e.g. static_cast is.

You must provide the exact type stored in the std::any object to access its value (via std::any_cast). It is impossible to imply any conversions.

Because the type stored in the std::any is only determined at runtime, the compiler would need to emit code for every possible type that could be stored in it and be convertible to the target type, if implicit conversions were allowed. That is clearly infeasible or impossible.

There are only very few situations where std::any is the right tool for the job. In particular it will not allow you to circumvent the statically-typed nature of C++.

暖伴 2025-02-18 08:39:40

我希望Any_cast(a)应该能够致电我的(a)来构建一个新对象,但实际上不是。

不,any_cast&lt; t&gt;(a)在且仅当t出现在a中时才成功。是否在a中有t回答查询。不是是否有某种类型隐式转换为t。在自己提取价值之后,您必须这样做。

I expected that any_cast(a) should be able to call My(a) to construct a new object, but actually not.

No, any_cast<T>(a) succeeds if and only if T is present in a. It answers the query whether there is T in a. Not whether there is some type implicitly convertible to T. You have to do that after extracting the value yourself.

上课铃就是安魂曲 2025-02-18 08:39:40

至于手册

投掷std :: bad_any_cast如果请求的t的类型不匹配操作数的内容。

a应包含精确的类型我的

我希望Any_cast(a)应该能够致电我的(a)来构建一个新对象,但实际上不是。

它不调用转换构造函数。

As for the manual

Throws std::bad_any_cast if the typeid of the requested T does not match that of the contents of operand.

a should contain the exact type My.

I expected that any_cast(a) should be able to call My(a) to construct a new object, but actually not.

It does not call converting constructors.

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