Is there an analogue of keyword "where" from C# in C++?

Viewed 128

I need to create a template class in C++. I need to make sure that the type for the template parameter will be a class with 1 int field and 1 string field (there can be more fields, but these are mandatory).

For example, in C# I could define an interface with methods or properties, like this:

interface MyInterface {
    int GetSomeInteger();
    string GetSomeString();
}

and then I could use it in my template class:

class MyClass<T> where T: MyInterface {}

Is there any way to do something like this in C++?

3 Answers

C++20 offers you the closest solution to C#:

#include <concepts>

template <class T>
concept MyInterface = requires(T x)
{
    { x.GetSomeInteger() } -> std::same_as<int>;
};

And then:

template <MyInterface T>
struct MyClass
{
    // ...
};

The most common way of doing this in current versions of C++ is a technique known as "duck-typing".

It simply involves just using T as if it implements the interface and let the compiler fail if you use the class with an incompatible type.

template<typename T>
class MyClass<T> {
  int foo() {
    T val;
    return val.GetSomeInteger();
  }
};

class Valid {
public:
  int GetSomeInteger() {return 0;}
};

class Invalid {
};

int main() {
  // works fine
  MyClass<Valid> a;
  a.foo();

  // fails to compile
  MyClass<Invalid> b;
  b.foo();
} 

Mind you, there ARE ways of enforcing this a bit more formally, but the amount of code involved is often not worth the benefit.

C++20 has concepts. Some compilers already support them. For example the following with gcc (trunk) -std=c++2a -fconcepts:

#include <string>
#include <iostream>
#include <concepts>

template<typename T>
concept HasGetIntAndString = requires(T& a) {
    { a.GetSomeInteger() } -> std::same_as<int>;
    { a.GetSomeString() } -> std::same_as<std::string>;
};

template <HasGetIntAndString T>
void bar(const T& t){
    std::cout << t.GetSomeInteger() << " " << t.GetSomeString();
}

struct foo {
    int GetSomeInteger() const { return 42; }
    std::string GetSomeString() const { return "some"; }
};

struct foo_not {
    std::string GetSomeInteger() { return "some"; }
    int GetSomeString() { return 42; }
};

int main(){
    bar( foo{});
    bar( foo_not{});
}

results in:

<source>: In function 'int main()':    
<source>:28:19: error: use of function 'void bar(const T&) [with T = foo_not]' with unsatisfied constraints   
   28 |     bar( foo_not{});    
      |                   ^    
<source>:12:6: note: declared here    
   12 | void bar(const T& t){    
      |      ^~~    
<source>:12:6: note: constraints not satisfied
<source>: In instantiation of 'void bar(const T&) [with T = foo_not]':    
<source>:28:19:   required from here    
<source>:6:9:   required for the satisfaction of 'HasGetIntAndString<T>' [with T = foo_not]    
<source>:6:30:   in requirements with 'T& a' [with T = foo_not]    
<source>:7:23: note: 'a.GetSomeInteger()' does not satisfy return-type-requirement    
    7 |     { a.GetSomeInteger() } -> std::same_as<int>;    
      |       ~~~~~~~~~~~~~~~~^~    
<source>:8:22: note: 'a.GetSomeString()' does not satisfy return-type-requirement    
    8 |     { a.GetSomeString() } -> std::same_as<std::string>;    
      |       ~~~~~~~~~~~~~~~^~    
cc1plus: note: set '-fconcepts-diagnostics-depth=' to at least 2 for more detail

Live Demo

Before C++20 you can use SFINAE. However, often it is simpler and more appropriate to not restrict the tempalte parameter more than necessary. If the template does call T::GetSomeInteger() but the type T has no such method, the template will already fail to compile without taking any further measures. SFINAE is mainly to provide nicer error messages.

Related