Skip to Content
C++ High Performance
book

C++ High Performance

by Viktor Sehr, Björn Andrist
January 2018
Intermediate to advanced
374 pages
9h 53m
English
Packt Publishing
Content preview from C++ High Performance

Usage example of is_detected and enable_if_t combined

As is_detected::value is a compile-time Boolean, is_detected can be combined with enable_if_t to enable a certain function for classes that contain a particular member function.

For example, we can implement a generic print function that can print both the to_string() method and the name_ member variable, depending on what the printed class has implemented:

namespace exp = std::experimental;template<typename T> using has_to_string = decltype(&T::to_string);template<typename T> using has_name_member = decltype(T::name_);// Print the to_string() function if it exists in classtemplate < typename T, bool HasToString = exp::is_detected<has_to_string,T>::value, bool HasNameMember = exp::is_detected<has_name_member,T>::value ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

C++ High Performance - Second Edition

C++ High Performance - Second Edition

Björn Andrist, Viktor Sehr
Advanced C++

Advanced C++

Gazihan Alankus, Olena Lizina, Rakesh Mane, Vivek Nagarajan, Brian Price
C++ In a Nutshell

C++ In a Nutshell

Ray Lischner
C++ Cookbook

C++ Cookbook

D. Ryan Stephens, Christopher Diggins, Jonathan Turkanis, Jeff Cogswell

Publisher Resources

ISBN: 9781787120952Supplemental Content