When should I use std::any

后端 未结 4 1817
悲哀的现实
悲哀的现实 2020-12-16 12:39

Since C++17 std::any is introduced. One can now write code like this

#include 
#include 
#include 

int         


        
4条回答
  •  南笙
    南笙 (楼主)
    2020-12-16 13:15

    When to Use
    void* as an extremely unsafe pattern with some limited use cases, std::any adds type-safety, and that’s why it has some real use cases.

    Some possibilities:

    • In Libraries - when a library type has to hold or pass anything without knowing the set of available types.
    • Parsing files - if you really cannot specify what are the supported types.
    • Message passing.
    • Bindings with a scripting language.
    • Implementing an interpreter for a scripting language
    • User Interface - controls might hold anything
    • Entities in an editor
      (ref)

提交回复
热议问题