Quantcast
Viewing all articles
Browse latest Browse all 22016

How do I statically check my templated class at declaration time?

In C# or Java, the following does not compile, because I "forgot" the where part in class declaration, that specifies that T is instance of something that defines the add method.

class C<T> {
    T make(T t) {
        return t.add(t);
    }
}

I'd like to get similar compile-time check in C++, if I specify incomplete requires for a template argument.

template <typename T>
requires true
class C {
public:
  T make() {
    return T{};
  }
};

I'd like to get a compile error for the C++ code above, stating that method c.make relies on T being default-constructible, which is however not captured in the requires constraints on T.

Can I get the compiler to check that my set of requires constraints is sufficient to cover everything the class implementation does?

I am using gcc (GCC) 10.0.0 20191207 (experimental) for this.


Viewing all articles
Browse latest Browse all 22016

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>