Is there any equivalent to boost::any in UE4?

Is there any equivalent to boost::any in UE4?

I am working with the ScriptPlugin and I’m trying to make a virtual method like this in FScriptBaseContext and since it is virtual I cannot template it:

virtual void SetFunctionParameter(boost::any variable) = 0;

If there is a way to do it with the libraries already provided by UE4, I would much rather go that route. While adding boost to a project shouldn’t be too difficult, I’d rather not bring in such a large dependency for something like this.

As far as I know there is no special any/var/QVariant etc. in UE4 but as it is ‘plain’ C++ you should be fine using boost::any/QVariant/your own if you desire?

You could just use boost::any without bring the whole boost library in. Have a look at boost bcp (boost copy). This only copies the barly needed files into your project.

Or if you just need some simple primitives, you can write your own:

struct VarType {
    // which type is in container
    enum Type {
        Integer,
        Double,
        Float,
        // etc
    }
    // the type
    Type type;

    // the values
    union Value {
        int asInt;
        int asDouble;
        int asFloat;
    }
};

// use
VarType intValue;
intValue.type = VarType::Integer;
intValue.asInt = 42;

// ..
switch (intValue.type) {
    case VarType::Integer:
        std::cout << "int value = " << intValue.asInt << std::endl;
    break;
// and so on
}

Completely untested code but you get the idea :wink:

Yeah, I was considering doing that too. I just try not to reinvent stuff that already exists, but I think in this case I might just have to make something simple :slight_smile: Thanks for the thoughts.

Unreal Engine 4 has similar data structure called FVariant. To quote the description:

Variant types can be used to store a range of different built-in types, as well as user defined types. The values are internally serialized into a byte array, which means that only FArchive serializable types are supported at this time.

It is not a template class but has a template constructor:

class FVariant
{
    template<typename T>
    FVariant( T InValue )
    {
        ...
    }
    ...
};

So it more like boost::any than boost::variant