element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Code Exchange
  • Technologies
  • More
Code Exchange
Blog modern C++: restrict templated types with concept
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Code Exchange to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: Jan Cumps
  • Date Created: 10 Mar 2025 7:30 PM Date Created
  • Views 344 views
  • Likes 3 likes
  • Comments 1 comment
  • Modern C++
  • c++26
  • c++23
  • c++
  • c++20
Related
Recommended

modern C++: restrict templated types with concept

Jan Cumps
Jan Cumps
10 Mar 2025

In C++, templates allow you to write generic code, that can handle many types. But almost always, there will be types you can't handle.

In my case, I have a callback manager that supports functions that return a void, a number, a bool or a class. In all those cases, it knows what to do. If you pass it something that's not one of those options, compilation will fail. And that's good .

The thing is, that when it fails, it tell you why in a difficult way. It 'll point to the template code and give some complex error:

[build] FAILED: callback_examples.cpp.o 
[build] In module callbackmanager, imported at callback_examples.cpp:8:
[build] callbackmanager.cpp: In instantiation of 
        ‘class callbackmanager::Callback@callbackmanager<char [], const int&, const int&>’:
[build] callback_examples.cpp:159:64:   required from here
[build]   159 |     callbackmanager::Callback<char[] , const int&, const int&> cb;

Although this is correct, it's hard to find out what's wrong. It would be more useful if the error informs the developer that the code is incompatible with the template (template constraint failure). That's what a concept does:

[build] FAILED: callback_examples.cpp.o 
[build] callback_examples.cpp: In function ‘int main()’:
[build] callback_examples.cpp:159:61: error: template constraint failure 
        for ‘template<class R, class ... Args>  
        requires  Callbackable<R> class callbackmanager::Callback@callbackmanager’
[build]   159 |     callbackmanager::Callback<char[], const int&, const int&> cb;

It doesn't make programs better, nor does it add to the size of the executable code. But it helps developers to use your generic code correctly.

Here is a concept that only allows the return value types that my template class can handle:  void, a number, a bool or a class:

// concept guards what types of return values we can handle
template<typename R>
concept Callbackable = 
	std::is_void<R>::value ||
	std::is_arithmetic_v<R> ||
	std::is_class_v<R>;

The type bool is part of the arithmetic family. It is covered by the second check.

In my class, I enforce that return values comply to the concept:

template <Callbackable R, typename... Args>
class Callback {
	using callbackfunction_t = std::function<R(Args...)>;	
public:
    // ...

That's it. A little bit of support for the consumers of your software. They get an easier experience. At no runtime costs.
And you as designer will get less support requests. The default error suggests that your template class has an issue. When using a concept, it informs the developer that they try something unsupported.

Thank you for reading.

  • Sign in to reply
  • Jan Cumps
    Jan Cumps 6 months ago

    Analogy:

    image

    source: https://www.ajc.com/gdpr.html

    Without concepts, the compiler would say: "the bridge you are driving under is not suited for trucks that don't lower their load."

    With concepts: "your truck will not fit under that bridge if you don't properly lower that load on the truck bed."

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube