将由 boost::variant 聚合的类型的对象传递给接受该 boost::variant 的函数

发布于 2024-10-19 08:27:32 字数 723 浏览 1 评论 0原文

假设我有:

class TypeA { };
class TypeB { };
typedef boost::variant<TypeA, TypeB> Type;
  1. 这没问题:

    void foo(Type t) { };
    
    int main(){
        A型a;
        foo(a);
    }
    
  2. 这不能编译:

    void foo(Type &t) { };
    
    int main(){
        A型a;
        foo(a);
    }
    

    错误:

    <块引用>

    “Type&”类型引用的初始化无效从 “TypeA”类型的表达式

  3. 这也无法编译:

    void foo(Type *t) { };
    
    int main(){
        A型a;
        foo(&a);
    }
    

    错误:

    <块引用>

    无法将参数“1”转换为“TypeA*”为“Type*”为“void” foo(类型*)'

有没有办法通过引用(如情况 2)或指针将 boost::variant 聚合的类型之一的实例传递给接受 boost::variant 的函数(如情况3)?

非常感谢!

Suppose I have:

class TypeA { };
class TypeB { };
typedef boost::variant<TypeA, TypeB> Type;
  1. This is ok:

    void foo(Type t) { };
    
    int main(){
        TypeA a;
        foo(a);
    }
    
  2. This does not compile:

    void foo(Type &t) { };
    
    int main(){
        TypeA a;
        foo(a);
    }
    

    with the error:

    invalid initialization of reference of type ‘Type&’ from
    expression of type ‘TypeA’

  3. Also this does not compile:

    void foo(Type *t) { };
    
    int main(){
        TypeA a;
        foo(&a);
    }
    

    with the error:

    cannot convert ‘TypeA*’ to ‘Type*’ for argument ‘1’ to ‘void
    foo(Type*)’

Is there a way to pass to a function that accepts a boost::variant an instance of one of the types aggregated by that boost::variant, either through a reference (as in case 2) or a pointer (as in case 3)?

Thank you very much!

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

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

发布评论

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

评论(2

对你而言 2024-10-26 08:27:32

1 中真正发生的情况:

TypeA a;
Type __temporary__(a);
foo(__temporary__);

2 或 3 中不会发生的情况:

TypeA a;
Type* __temporary__(&a);
  // this fails because there is no inheritance relationship
foo(__temporary__);

您有两种解决方案(对于非模板 foo):

  • 转换为 Type,然后获取对此的指针/引用,
  • 创建一个 boost::variant 用于隐式转换以启动

第三种解决方案是更改 foo 本身,并将其设为模板。这取决于你想做什么。

What really happens in 1:

TypeA a;
Type __temporary__(a);
foo(__temporary__);

What cannot happen in 2 or 3:

TypeA a;
Type* __temporary__(&a);
  // this fails because there is no inheritance relationship
foo(__temporary__);

You have two solutions (for a non template foo):

  • convert to Type, then take a pointer/reference to this
  • create a boost::variant<TypeA*,TypeB*> for implicit conversion to kick in

A third solution is to change foo itself, and make it template. It depends on what you want to do.

时光磨忆 2024-10-26 08:27:32

聚合意味着 boost::variant 包含 TypeATypeB。事实并非如此。它包含 TypeATypeB。它更像是一个联合而不是一个结构。

您可以按值传递 TypeA,因为存在从 TypeAType 的隐式转换。

没有从 TypeA&Type&(或 TypeA*Type*)的隐式转换,并且不应该有不会的。想象一下,如果将对 TypeA 对象的引用传递给 foo() 并且 foo() 决定将其替换为 代码>TypeB 值。

在不知道 foo()TypeA/TypeB 是什么的情况下,我无法给您更具体的建议,但也许您可以使用函数模板。即

template <typename T>
void foo(T& t) {}

或重载函数:

void foo(TypeA& t) {}
void foo(TypeB& t) {}

Aggregation implies that the boost::variant contains both TypeA and TypeB. It doesn't. It contains either TypeA or TypeB. It's more like a union than a struct.

You can pass TypeA by value because there is an implicit conversion from TypeA to Type.

There is no implicit conversion from TypeA& to Type& (or TypeA* to Type*) and there shouldn't be. Think of what would happen if a reference to a TypeA object was passed into foo() and foo() decides to replace it with a TypeB value.

Without knowing what foo() and TypeA/TypeB are, I can't give you more specific advice but perhaps you can use a function template. i.e.

template <typename T>
void foo(T& t) {}

or overloaded functions:

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