What is the typical usage of boost any library?

前端 未结 5 1194
暗喜
暗喜 2020-12-30 00:44

What are the advantages of using boost.any library ? Could you please give me some real life examples ? Why the same functionality couldn\'t be achieved by having some gener

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-30 01:13

    We use boost.any as the carrier type for a type-safe tagged variadic container. Here's what that means:

    We have a "raft" object, which travels through a set of filters. When a filter wants to add data to the raft, it can do something like this:

    raft.addTaggedData(3.0);
    raft.addTaggedData("a string")`;
    std::string str = raft.getTaggedData();
    int a = raft.getTaggedData(); // <-- Compile error
    

    Where ETag1 and ETag2 are members of an enum, and we use a traits template to map tags to types.

    The raft class is using a list of pair as a backing store. Boost.any saved us the pain of managing raw buffers for various types.

提交回复
热议问题