Mathieu Van Nevel Mathieu Van Nevel - 5 days ago 5
C++ Question

C++ Concepts : invalid reference to function concept

I tried this small piece of code that I modified from the Concepts Technical Specification:

template < typename T >
concept bool C_Object() {
return requires {
T();
};
}

template < typename Object >
requires C_Object<Object>
class Foo {
public:
Object test;
};

struct Component {
int data;
Component() : data(0) {}
};

int main() {
Foo<Component> test;
return 0;
}


But I get this error :

test.cpp:10:12: error: invalid reference to function concept ‘template<class T> concept bool C_Object()’
requires C_Object<Object>
^~~~~~~~~~~~~~~~
test.cpp: In function ‘int main()’:
test.cpp:26:16: error: template constraint failure
Foo<Component> test;
^
test.cpp:26:16: note: constraints not satisfied
test.cpp:26:16: note: ill-formed constraint


First time I try the new version of C++ concepts, where I am missing?

Thanks

Have a great day

Answer

This definition:

template < typename T >
concept bool C_Object() {
  return requires {
    T();
  };
}

defines C_Object as a function concept. This one:

template < typename Object >
requires C_Object<Object>
class Foo {
public:
  Object test;
};

uses C_Object as if it were a variable concept. In a requires clause, you must use () to "invoke" function concepts:

template < typename Object >
requires C_Object<Object>()
class Foo {
public:
  Object test;
};

Alternatively, you could use the "terse" placeholder syntax to constrain Object which does not differentiate variable and function concepts:

template < C_Object Object >
class Foo {
public:
  Object test;
};
Comments