boost::enable_if Deep Down Boostimagical Fun Christian Bay and Kasper A Andersen Department of Computer Science University of Copenhagen

Similar documents
TDDD38 - Advanced programming in C++

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

Using Enum Structs as Bitfields

Lecture 2 Polymorphism, Traits, Policies, and Inheritance

Function Overloading

Intermediate Programming, Spring 2017*

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

l Operators such as =, +, <, can be defined to l The function names are operator followed by the l Otherwise they are like normal member functions:

Template metaprogramming techniques for concept-based specialization

Variables. Data Types.

COMP 2355 Introduction to Systems Programming

static CS106L Spring 2009 Handout #21 May 12, 2009 Introduction

Lecture 7. Log into Linux New documents posted to course webpage

! Operators such as =, +, <, can be defined to. ! The function names are operator followed by the. ! Otherwise they are like normal member functions:

CSCE 110 PROGRAMMING FUNDAMENTALS

Homework 5. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

CSCI 104 Templates. Mark Redekopp David Kempe

MODERN AND LUCID C++ ADVANCED

05-01 Discussion Notes

template <typename T> // unless it s an unqualified pointer struct IsPtr<T *> { enum { r = true }; };

Test- Case Reduc-on for C Compiler Bugs. John Regehr, Yang Chen, Pascal Cuoq, Eric Eide, Chucky Ellison, Xuejun Yang

GEA 2017, Week 4. February 21, 2017

Fast Introduction to Object Oriented Programming and C++

Overload Resolution. Ansel Sermersheim & Barbara Geller ACCU / C++ June 2018

Operator Overloading and Templates. Linear Search. Linear Search in C++ second attempt. Linear Search in C++ first attempt

Class and Function Templates

Motivation for Templates. Class and Function Templates. One Way to Look at Templates...

Introducing C++ to Java Programmers

Linked List using a Sentinel

! Search: find a given target item in an array, ! Linear Search: Very simple search method: ! Operators such as =, +, <, and others can be

CMSC 341 Lecture 6 Templates, Stacks & Queues. Based on slides by Shawn Lupoli & Katherine Gibson at UMBC

B-Trees. nodes with many children a type node a class for B-trees. an elaborate example the insertion algorithm removing elements

CSCI 104 Templates. Mark Redekopp David Kempe

Modern and Lucid C++ Advanced for Professional Programmers. Part 12 Advanced Library Design. Department I - C Plus Plus Advanced

Lists. linking nodes. constructors. chasing pointers. MCS 360 Lecture 11 Introduction to Data Structures Jan Verschelde, 17 September 2010.

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

Motivation for Templates

PIC 10A. Lecture 17: Classes III, overloading

8. The C++ language, 1. Programming and Algorithms II Degree in Bioinformatics Fall 2017

Modern and Lucid C++ Advanced for Professional Programmers. Part 7 Compile-Time Computation. Department I - C Plus Plus

C++1z: Concepts-Lite !!! !!! !!!! Advanced Developer Conference C slides:


Macros and Preprocessor. CGS 3460, Lecture 39 Apr 17, 2006 Hen-I Yang

C++ Programming Lecture 6 Software Engineering Group

CSI33 Data Structures

Static If I Had a Hammer

Debug C++ Without Running. Anastasia Kazakova

A brief introduction to C++

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.

Due Date: See Blackboard

Programming Language. Functions. Eng. Anis Nazer First Semester

CS93SI Handout 04 Spring 2006 Apr Review Answers

PIC 10A Objects/Classes

Overloading Operators in C++

Programming. C++ Basics

Programming Abstractions

Object-Oriented Programming in C++

Structuur van Computerprogramma s 2

W3101: Programming Languages C++ Ramana Isukapalli

Creating a C++ Program

CPSC 427: Object-Oriented Programming

Part X. Advanced C ++

Pointers, Dynamic Data, and Reference Types

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A

CS 247: Software Engineering Principles. C++ Templates. Reading: Eckel, Vol. 2 Ch. 5 Templates in Depth. U Waterloo CS247 (Spring 2017) p.

An Introduction to Template Metaprogramming

CSI33 Data Structures

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

Botet C++ generic match function P0050

Short Notes of CS201

Lab 1: First Steps in C++ - Eclipse

P1267R0: Custom Constraint Diagnostics

C++: Overview and Features

@odinthenerd. not the god. Auto-Intern GmbH 1

COSC 320 Exam 2 Key Spring Part 1: Hash Functions

Implementing an ADT with a Class

CS201 - Introduction to Programming Glossary By

Module 7 b. -Namespaces -Exceptions handling

C++ Template Metaprogramming

Introduction to metaprogramming. Nicolas Burrus

Lab 2: ADT Design & Implementation

Operator overloading

CSE 333 Final Exam 3/19/14

Part III. Advanced C ++

Object Oriented Design

Semantics of C++ Hauptseminar im Wintersemester 2009/10 Templates

Understand Execution of a Program

The Structure of a C++ Program

Unit 7. 'while' Loops

Object-Oriented Programming for Scientific Computing

Comp151. Generic Programming: Container Classes

How the Adapters and Binders Work

typedef Labeling<unsigned char,short> LabelingBS; typedef Labeling<unsigned char,short>::regioninfo RegionInfoBS;

Templates (again) Professor Hugh C. Lauer CS-2303, System Programming Concepts

Queue Implementations

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

CSCE 110 PROGRAMMING FUNDAMENTALS

Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering

Overload Resolution. Ansel Sermersheim & Barbara Geller Amsterdam C++ Group March 2019

Transcription:

Deep Down Boostimagical Fun boost::enable_if Christian Bay and Kasper A Andersen Department of Computer Science University of Copenhagen C. Bay and K. A. Andersen, June 2, 2006 p. 1/12

SFINAE revisited What is SFINAE? Easy: "Substitution Failure Is Not An Error" Example: void dodah( int i ) // Version 1 cout << "I: " << i << endl; } template <typename T> void dodah( T i ) // Version 2 typename T::result_type result_type; cout << "I: " << i << endl; } C. Bay and K. A. Andersen, June 2, 2006 p. 2/12

SFINAE revisited (contd.) What happens now? int main() int i; some_class_with_result_type c; short s; dodah( i ); // Version 1 called dodah( c ); // Version 2 called dodah( s ); //??? } return 0; C. Bay and K. A. Andersen, June 2, 2006 p. 3/12

SFINAE revisited (contd.) Visual Studio is not really happy: Examples.h(17) : error C2653: T : is not a class or namespace name; main.cpp(13) : see reference to function template instantiation void dodah<short>(c) with [C=short] being compiled Examples.h(17) : error C2065: result_type : undeclared identifier Examples.h(17) : error C2146: syntax error : missing ; before identifier result_type Examples.h(17) : error C3861: result_type : identifier not found, even with argument-dependent lookup C. Bay and K. A. Andersen, June 2, 2006 p. 4/12

SFINAE revisited (contd.) By reorganizing a little, SFINAE kicks in: template <typename T> typename T::result_type* dodah( T i ) cout << "I: " << i << endl; return 0; } // Alternatively template <typename T> void dodah( T i, typename T::result_type* p=0 ) cout << "I: " << i << endl; } C. Bay and K. A. Andersen, June 2, 2006 p. 5/12

Using enable if Let s try to use enable_if: template <typename T> void dodah( T i, typename disable_if<is_integral<t> >::type* p=0 ) cout << "I: " << i << endl; } This is our new main template. All integral types goes to version 1. C. Bay and K. A. Andersen, June 2, 2006 p. 6/12

Using enable if (contd.) What about classes? template <typename T, typename Enable = void> class Dodah // General implementation }; template <typename T> class Dodah <T, typename enable_if<is_integral<t> >::type> // Special implementation for integral types }; C. Bay and K. A. Andersen, June 2, 2006 p. 7/12

Implementation of enable if template <bool B, class T = void> struct enable_if_c typedef T type; } template <class T> struct enable_if_c<false, T> } // Wrapper extracting value from Cond template <class Cond, class T = void> struct enable_if : public enable_if_c<cond::value, T> } Similarly, we have disable_if. And then we have the lazy cousin... C. Bay and K. A. Andersen, June 2, 2006 p. 8/12

Lazy enable if Remember lazy eveluation? enable_if has something similar: template <bool B, class T> struct lazy_enable_if_c typedef typename T::type type; } template <class T> struct lazy_enable_if_c<false, T> // ::type is not extracted if condition is false }; template <class Cond, class T> struct lazy_enable_if : public lazy_enable_if_c<cond::value, T> }; C. Bay and K. A. Andersen, June 2, 2006 p. 9/12

Using the lazy version template <typename T> typename enable_if< is_integral<t>, typename int_traits<t>::type>::type dodah( T& const t ) // Implementation } template <typename T> typename lazy_enable_if< is_integral<t>, int_traits<t> >::type dodah( T& const t ) // Implementation } The first one fails if int_traits<t>::type is not defined even though is_integral<t> is false. SFINAE is not used since invalid type occurs as an argument to another template! C. Bay and K. A. Andersen, June 2, 2006 p. 10/12

Not portable Not all compilers support SFINAE. Boost has a fine macro that helps disabling boost enable_if on those compilers. // Even the definition of enable_if causes problems on some compilers, // so it s macroed out for all compilers that do not support SFINAE #ifndef BOOST_NO_SFINAE... #else... template <class Cond, class T = detail::enable_if_default_t> struct enable_if : enable_if_does_not_work_on_this_compiler<t> };... #endif // BOOST_NO_SFINAE C. Bay and K. A. Andersen, June 2, 2006 p. 11/12

Not portable (cont.) A nice error message instead? error C2504: boost::enable_if_does_not_work_on_this_compiler<t> with [T=boost::detail::enable_if_default_T] : base class undefined; see reference to class template instantiation boost::enable_if_does_not_work_on_this_compiler<t> with [T=boost::detail::enable_if_default_T] being compiled;... Our conclusion: Rethink your design if you really need enable_if. Use different function names, full template specializations (that calls the right specialized function to minimize the code that needs to be maintained). C. Bay and K. A. Andersen, June 2, 2006 p. 12/12