Assignment #1 Advanced Programming in C /2018. NPRG051 Advanced programming in C++ - Assignment #1

Similar documents
C++ Important Questions with Answers

CS110: PROGRAMMING LANGUAGE I

Expansion statements. Version history. Introduction. Basic usage

C++ Exception Handling 1

Basic Types, Variables, Literals, Constants

Chapter 1. C++ Basics. Copyright 2010 Pearson Addison-Wesley. All rights reserved

Programming with C++ as a Second Language

This watermark does not appear in the registered version - Slide 1

CS

CSE 431S Type Checking. Washington University Spring 2013

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

Structured Programming Using C++ Lecture 2 : Introduction to the C++ Language. Dr. Amal Khalifa. Lecture Contents:

register lock_guard(mtx_); string_view s = register to_string(42); We propose register-expression to grant the temporary objects scope lifetimes.

Introduction to Programming

Using Enum Structs as Bitfields

New Tools for Class and Library Authors

The SPL Programming Language Reference Manual

Overview. 1. Expression Value Categories 2. Rvalue References 3. Templates 4. Miscellaneous Hilarity 2/43

Tokens, Expressions and Control Structures

GCC : From 2.95 to 3.2

CS93SI Handout 04 Spring 2006 Apr Review Answers

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

Part X. Advanced C ++

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

The PCAT Programming Language Reference Manual

Interview Questions of C++

COMP322 - Introduction to C++

Operator Dot Wording

TDDD38 - Advanced programming in C++

2.9 DCL58-CPP. Do not modify the standard namespaces

Functions and Recursion

Chapter 15 - C++ As A "Better C"

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

IS 0020 Program Design and Software Tools

Introduction to C++ Systems Programming

Intermediate Programming, Spring 2017*

Other C++11/14 features

Outline. 1 Function calls and parameter passing. 2 Pointers, arrays, and references. 5 Declarations, scope, and lifetimes 6 I/O

Fast Introduction to Object Oriented Programming and C++

Structured bindings with polymorphic lambas

Botet C++ generic match function P0050

C The new standard

Chapter-8 DATA TYPES. Introduction. Variable:

Pointers and Strings Prentice Hall, Inc. All rights reserved.

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

Modernes C++ Träume und Alpträume

C++14 Reflections Without Macros, Markup nor External Tooling

class Polynomial { public: Polynomial(const string& N = "no name", const vector<int>& C = vector<int>());... };

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

Abstraction and Encapsulation. Benefits of Abstraction & Encapsulation. Concrete Types. Using Typedefs to streamline classes.

Exceptions. CS162: Introduction to Computer Science II. Exceptions. Exceptions. Exceptions. Exceptions. Exceptions

Modern and Lucid C++ Advanced for Professional Programmers. Part 3 Move Semantics. Department I - C Plus Plus Advanced

Your first C++ program

Before we dive in. Preprocessing Compilation Linkage

Software Engineering Concepts: Invariants Silently Written & Called Functions Simple Class Example

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

APPENDIX A : KEYWORDS... 2 APPENDIX B : OPERATORS... 3 APPENDIX C : OPERATOR PRECEDENCE... 4 APPENDIX D : ESCAPE SEQUENCES... 5

CSCI 102L - Data Structures Midterm Exam #1 Fall 2011

Ch. 12: Operator Overloading

Template Issue Resolutions from the Stockholm Meeting

EXP54-CPP. Do not access an object outside of its lifetime

CPSC 427: Object-Oriented Programming

The format for declaring function templates with type parameters

C++11 Introduction to the New Standard. Alejandro Cabrera February 1, 2012 Florida State University Department of Computer Science

Introduction to Core C++

Programming in C++ 6. Floating point data types

PHY4321 Summary Notes

Modern and Lucid C++ Advanced for Professional Programmers. Part 1 C Plus Plus Recap. Department I - C Plus Plus

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

Exception Safe Coding

Working Draft, Extensions to C++ for Modules

METAPROGRAMMING FOR DUMMIES LOUIS DIONNE, C++NOW 2016

Copyie Elesion from the C++11 mass. 9 Sep 2016 Pete Williamson

Adding attribute reflection to C++.

Structuur van Computerprogramma s 2

Type Erasure. Nevin :-) Liber Friday, October 29, Chicago C/C++ Users Group Nevin J. Liber All Rights Reserved.

Chapter 2 - Control Structures

C++ TEMPLATES. Templates are the foundation of generic programming, which involves writing code in a way that is independent of any particular type.

Abstract This paper proposes the ability to declare multiple variables initialized from a tuple or struct, along the lines of:

Operator Overloading in C++ Systems Programming

CEC Intermediate Representation

Modern and Lucid C++ for Professional Programmers. Week 5 Classes and Operators. Department I - C Plus Plus

Programmazione. Prof. Marco Bertini

17. Classes. Overloading Functions. Operator Overloading. Function Overloading. operatorop

Lambdas in unevaluated contexts

QStringView. everywhere. Marc Mutz, Senior Software Engineer at KDAB

Advanced C++ Topics. Alexander Warg, 2017

Value categories. PRvalues. Lvalues

A Fast Review of C Essentials Part I

Console input 26/09/2018. Background. Background

Programming at Compile Time. Rainer Grimm Training, Coaching, and Technology Consulting

Templates. Zoltán Porkoláb: C++11/14 1

EL2310 Scientific Programming

Slide Set 14. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

C++11/14 Rocks. Clang Edition. Alex Korban

Operator Dot Wording

Rvalue References & Move Semantics

Cpt S 122 Data Structures. Introduction to C++ Part II

Jim Lambers ENERGY 211 / CME 211 Autumn Quarter Programming Project 4

Transcription:

Assignment #1 Advanced Programming in C++ 2017/2018 1

Assignment #1 split deadline: Thu 12 Apr 23:59 2

using namespace splitter; std::istringstream iss("alpha:=10/50.1"); std::string x; int y; double z; iss >> split(x, ':', '=', y, '/', z); std::cout << "x = " << x << ", y = " << y << ", z = " << z << std::endl; Implement the function split In the namespace splitter Any number of arguments: R-value of type char L-value of any type T The operator >> must be defined for an L-value of type std::istream and and an L-value of type T Two L-values must never be adjacent The example prints x = alpha, y = 10, z = 50.1 3

using namespace splitter; std::istringstream iss("alpha:=10/50.1"); std::string x; int y; double z; iss >> split(x, ':', '=', y, '/', z); std::cout << "x = " << x << ", y = " << y << ", z = " << z << std::endl; Functionality When the result of split is used as the right operand of >> on an std::istream & Each L-value argument A is used (left-to-right) as follows: The input stream is read until the delimiter character or the EOF is reached The characters read (except the delimiter) are fed into the >> operator with A as the right operand If the >> operator fails or if it does not read all the characters, the operation throws an exception The delimiter is the next argument it must be an R-value of type char if there is no next argument, \n is used The characters including the delimiter are always removed (read) from the input stream 4

using namespace splitter; std::istringstream iss("alpha:=10/50.1"); std::string x; int y; double z; iss >> split(x, ':', '=', y, '/', z); std::cout << "x = " << x << ", y = " << y << ", z = " << z << std::endl; Functionality Each L-value argument A is used (left-to-right) as follows: The input stream is read until the delimiter character or the EOF is reached The delimiter is the next argument it must be an R-value of type char if there is no next argument, \n is used The characters including the delimiter are always removed (read) from the input stream For each R-value argument of type char which is not right to an L-value A character is read from the input stream and compared to the argument If there is mismatch or EOF, the operation throws an exception 5

using namespace splitter; std::istringstream iss("alpha:=10/50.1"); std::string x; int y; double z; Compile-time diagnostics A compilation error (preferably a static_assert) shall be triggered if Two L-value arguments are adjacent, or An R-value argument is of type different than char iss >> split(x, ':', '=', y, '/', z); std::cout << "x = " << x << ", y = " << y << ", z = " << z << std::endl; 6

using namespace splitter; std::istringstream iss("alpha:=10/50.1"); std::string x; int y; double z; iss >> split(x, ':', '=', y, '/', z); std::cout << "x = " << x << ", y = " << y << ", z = " << z << std::endl; Run-time diagnostics An exception (preferably a std::logic_error) shall be thrown if An explicitly specified delimiter (i.e. an R-value argument right to an L- value) is not found in (the remainder of) the input stream The conversion (by >>) of the characters encountered before the explicit or implicit delimiter fails or does not eat up all the characters An additional R-value argument (not right to an L-value) is not present as the next character in the input stream 7

Assignment #1 split Technical details 8

Put everything into a file named test01split.hpp Don t forget the inline keyword for all global functions/operators Use namespace splitter for all public parts The type returned by split Use whatever name you want The function split The operator>> defined for the type returned by split The operator will be found using argument-dependent lookup It s a good idea to put everything else into an unpublished namespace, e.g. splitter_impl NPRG051 Pokročilé programování v C++ - 2015/2016 - DÚ 9

Use recodex to submit and test the functionality of your solutions Submit only test01split.hpp to recodex recodex uses g++ 7.3 The points will be awarded based on manual inspection of the source codes submitted to recodex NPRG051 Pokročilé programování v C++ - 2015/2016 - DÚ 10

Assignment #1 split Hints 11

Hints To distinguisth lvalues from rvalues, observe what forwarding reference does: template< typename T> void f( T && p) If the actual argument is an rvalue of type U then T = U If the actual argument is an lvalue of type U then T = U & For performance, avoid repeated passing of parameter lists Pack them into a tuple Compare what the following tuple declarations do: template< typename... T> void f( T &&... p) { using tuple1 = std::tuple< std::remove_reference_t< T>...>; using tuple2 = std::tuple< T...>; using tuple3 = std::tuple< T &&...>; And observe the effect of the following statements: auto data1 = std::make_tuple( std::forward<t>(p)...); data1 contains a copy of argument values, its type is tuple1 (except for array arguments) auto data2 = std::tie(p...); } data2 contains a reference to argument values, its type is std::tuple< T &...> NPRG051 Pokročilé programování v C++ - 2015/2016 - DÚ 12

Hints The packed tuple shall be passed by reference The type list used in the declaration of the tuple cannot be altered In recursive implementations, two options are available Indexing template< std::size_t I, typename... T2> void f( std::tuple< T2...> & p) { // solve the I-th element of the tuple, then call the rest f< I+1>( p); } Two type lists template< typename... T> struct S { template< typename... T2> static void f( std::tuple< T2...> & p); }; Use partial specialization of struct S to remove front elements of the type list T NPRG051 Pokročilé programování v C++ - 2015/2016 - DÚ 13