mercredi 1 juillet 2015

Emit warning if template parameter does not satisfy certain concepts

Given a templated type (class, struct, whatever) and I want to impose certain properties on the template parameter T.

For example, I can use static_assert(std::is_copy_assignable<T>::value, "T must be copy-assignable") to enforce T satisfies the CopyAssignable concept. Instantiating the templated type with a non-copy-assignable type T will result in a compile error.

But how can I achieve emitting a compile warning (no error) if T does not satisfy e.g. MoveConstructible (std::is_move_constructible<T>::value). This would be useful to notify the user of an API that this property is desirable but not required to have on (probably the users' own) type T.

I though of using std::conditional<std::is_move_constructible<T>::value, [no_warning], [warning]>, but couldn't find a way to emit a compiler warning for the false case.

Note: I can only use the C++11 standard (so no C++14/17 features, though I'd be interested for curiosity in solutions using these) and only care about GCC, Clang and Intel compilers.

Aucun commentaire:

Enregistrer un commentaire