Skip to content

Regression: ofParameter<ParameterType> fails to compile if ParameterType lacks operator== due to isInit() #8294

@eduardfrigola

Description

@eduardfrigola

Hi!

While testing for the 0.12.1 release, I found a regression in ofParameter related to the newly introduced ::isInit() method in commit e2bb65c

Currently, for this method to compile, ParameterType must implement operator==

bool ofParameter<ParameterType>::isInit() const {
return obj->value == obj->init;
}

Minimal example to reproduce issue.

//Does not compile
struct foo{
    int x;
};
ofParameter<foo> pFoo;

//Does compile
struct bar{
    int x;
    bool operator==(const bar& other) const{return x == other.x;}
};
ofParameter<bar> pBar;

In 0.12.0, both foo and bar compiled without issues.

I realize my use case for ofParameter goes beyond its intended design (I'm using it in ofxOceanode for sharing data between nodes). Some past issues I've encountered with ofParameter (such as #6576) have been related to this extended usage.

If you think this regression should be addressed, one potential approach could be similar to how ::fromString / ::toString handle types that lack stream operators.

Relevant parts of the ::fromString implementation:

template <typename ParameterType>
inline void ofParameter<ParameterType>::fromString(const std::string & str) {
try {
set(of::priv::fromStringImpl<ParameterType>(str));
} catch (...) {
ofLogError("ofParameter") << "Trying to de-serialize non-serializable parameter";
}
}

template <typename ParameterType>
typename std::enable_if<of::priv::has_loading_support<ParameterType>::value, ParameterType>::type fromStringImpl(const std::string & str) {
return ofFromString<ParameterType>(str);
}

template <typename T>
struct has_loading_support {
static std::istream & stream;
static T & x;
static constexpr bool value = sizeof(check_op(stream >> x)) == sizeof(yes);
};

cc @alexandreburton @artificiel What do you think? Would you like me to submit a PR to implement this approach?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions