Edited at

C++ Templates (6)3.2 Use of Class Template Stack stack1test.cpp

More than 1 year has passed since last update.

C++ Templates by David Vandevoorde, Nicolai M Josuttis, ISBN 978-0321714121, Addison-Wesley Professiona

http://www.tmplbook.com

C++ Templates The Complete Guide(2nd Edition)をclang++とg++でコンパイルしてみた

https://qiita.com/kaizen_nagoya/items/a7065ea839cb33793bdf


Part1 BASICS


Chapter 3. Class Templates


3.2 Use of Class Template Stack stack1test.cpp

http://www.tmplbook.com/code/tmplbook-code.tar

1 filename: Te_ba_stack1test.cpp

2 original examples and/or notes:

3 compile and output mechanism:

4 compile errors and/or warnings:

5. Hardware: MacBook Pro, (Retina, 13-inch, Mid 2014)

6. Special Thanks: Upper organizatios and

7. notes

8. source code

9. Error, Warning and/or Output


算譜(source code)


T_ba_stack1test.cpp

// 1 filename: Te_ba_stack1test.cpp

// ver 0.1 8(Sun), Oct, 2017

#include <iostream>
#include <cstring>
#include <string>
#include <cstdlib>

// 2 original examples and/or notes:
// (c) Copyright 2003 "C++ Templates - The Complete Guide" by Pearson Education, Inc. David Vandevoorde, and Nicolai M. Josuttis. All rights reserved.
// http://www.josuttis.com/tmplbook/
//The best way to reach the author of the book is by e-mail:tmplbook@josuttis.com
std::string ru ("\"C++ Templates Part1 BASICS.\"");
std::string rus ("\"Chapter 3. Class Templates 3.2 Use of Class Template Stack stack1test.cpp \"");

// 3 compile and output mechanism:
// (c) Dr. OGAWA Kiyoshi, twitter @kaizen_nagoya e-mail:kaizen at wh.commufa.jp
// https://researchmap.jp/jo0n1csau-1797580/#_1797580
//
// 4 compile errors and/or warnings:
// 4.1 Clang http://www.llvm.org/
// Copyright (c) 2003-2017, LLVM Project
// clang++ --version
// (c) clang version 5.0.0 (tags/RELEASE_500/final)
// Target: x86_64-apple-darwin14.5.0
// Thread model: posix
// InstalledDir: /usr/local/opt/llvm/bin
// Base standard: https://clang.llvm.org/cxx_status.html
// Command/Options: clang++ -std=c++17 -stdlib=libc++ -Wall Te_ba_stack1test.cpp -o Te_ba_stack1testL
// Configuration: brew install --with-clang llvm
//
// 4.2. g++-7 (Homebrew GCC 7.3.0_1) 7.3.0
// Copyright (C) 2017 Free Software Foundation, Inc.
// This is free software; see the source for copying conditions.
// There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// ln -s /usr/local/bin/gcc-7 /usr/local/bin/gcc
// ln -s /usr/local/bin/g++-7 /usr/local/bin/g++
// Base standard: http://gcc.gnu.org/onlinedocs/gcc/Standards.html
// Command/Options: g++-7 -std=c++17 -Wall Te_ba_stack1test.cpp -o Te_ba_stack1testG
// Configuration:brew install gcc7
//
// 5. Hardware: MacBook Pro, (Retina, 13-inch, Mid 2014)
// OS X 10.10.5 Yosemite
//Core i5, 2.6GHz, 16GB, 1600MHz DDR3
//(c) Intel http://ark.intel.com/products/series/75024/4th-Generation-Intel-Core-i5-Processors
//
// 6. Special Thanks: Upper organizatios and
// ISO/IEC JTC1 SC22 WG21: http://www.open-std.org/jtc1/sc22/wg21/
// MISRA: https://www.misra.org.uk/forum/index.php
// ISO/IEC JTC1 SC22 WG14: http://www.open-std.org/jtc1/sc22/wg14/
// ITSCJ/IPSJ http://www.itscj.ipsj.or.jp/itscj_english/index.html
// Renesas Electronics Corporation.http://www.renesas.com/
// NPO SESSAME project, http://www.sessame.jp/workinggroup/WorkingGroup3/
// Toyo Corporation, http://www.toyo.co.jp/English/
// Japan Standard Association, http://bit.ly/1lzykg1
// NPO TOPPERS project, https://www.toppers.jp/asp-d-download.html
// Daido Universcity, http://www.daido-it.ac.jp/gakubugakka/computer/index.html
// WITZ Co.Ltd., http://www.witz-inc.co.jp/products/solution/solution.html
// SevenWise.Co., http://www.7ws.co.jp/index.html
// TOYOTA Motor Corporation, http://toyota.jp/
// DENSO Corporation, http://www.globaldenso.com/en/
// Aisin Seiki Co. Ltd., http://www.aisin.com/
// Cypress Semiconductor Corp., http://japan.cypress.com/
// Yazaki Corporation, http://www.yazaki-group.com/global/
// Pananosic Corporation, http://www.panasonic.net/
// Denso Create Inc.: http://www.denso-create.jp
// SWEST: Summer Workshop on Embedded System Technologies , http://swest.toppers.jp
// CEST: Consortium for Embedded System Technology, http://www.ertl.jp/CEST/
// JUSE: Union of Japanese Scientists and Engineers, http://www.juse.or.jp/e/
// OSC: Open Source Conference, http://www.ospn.jp/

// 7. notes
//{T} C++ Templates
//{L} LLVM warnings and errors
//{G} Gcc warning and errors
//{S} C++ standard, ISO/IEC JTC1 SC22 WG14
// add 'n' to function and variable names of non compliant example if a same name exists. And sequence number also.
// add 'c' to function and variable names of compliant examples if a same name exists. And sequence number also.

// 8. source code
/* The following code example is taken from the book
* "C++ Templates - The Complete Guide"
* by David Vandevoorde and Nicolai M. Josuttis, Addison-Wesley, 2002
*
* (C) Copyright David Vandevoorde and Nicolai M. Josuttis 2002.
* Permission to copy, use, modify, sell and distribute this software
* is granted provided this copyright notice appears in all copies.
* This software is provided "as is" without express or implied
* warranty, and with no claim as to its suitability for any purpose.
*/

// stack1test.cpp
#include "stack1.hpp"

int main(int argc, char* argv[])/// add argc and argv
{
try {
Stack<int> intStack; // stack of ints
Stack<std::string> stringStack; // stack of strings

// manipulate int stack
intStack.push(7);
std::cout << intStack.top() << std::endl;

// manipulate string stack
stringStack.push("hello");
std::cout << stringStack.top() << std::endl;
stringStack.pop();
stringStack.pop();
}
catch (std::exception const& ex) {
std::cerr << "Exception: " << ex.what() << std::endl;
return EXIT_FAILURE; // exit program with ERROR status
}
//}
std::cout << ru << std::endl << rus << std::endl;///
return EXIT_SUCCESS;
}



stack1.hpp

/* The following code example is taken from the book

* "C++ Templates - The Complete Guide"
* by David Vandevoorde and Nicolai M. Josuttis, Addison-Wesley, 2002
*
* (C) Copyright David Vandevoorde and Nicolai M. Josuttis 2002.
* Permission to copy, use, modify, sell and distribute this software
* is granted provided this copyright notice appears in all copies.
* This software is provided "as is" without express or implied
* warranty, and with no claim as to its suitability for any purpose.
*/

#include <vector>
#include <stdexcept>

template <typename T>
class Stack {
private:
std::vector<T> elems; // elements

public:
void push(T const&); // push element
void pop(); // pop element
T top() const; // return top element
bool empty() const { // return whether the stack is empty
return elems.empty();
}
};

template <typename T>
void Stack<T>::push (T const& elem)
{
elems.push_back(elem); // append copy of passed elem
}

template<typename T>
void Stack<T>::pop ()
{
if (elems.empty()) {
throw std::out_of_range("Stack<>::pop(): empty stack");
}
elems.pop_back(); // remove last element
}

template <typename T>
T Stack<T>::top () const
{
if (elems.empty()) {
throw std::out_of_range("Stack<>::top(): empty stack");
}
return elems.back(); // return copy of last element
}



編纂・実行結果(compile and go)


  1. Error, Warning and/or Output


cppall.sh

$ ../cppall.sh Te_ba_stack1test

$ clang++ Te_ba_stack1test.cpp -std=c++03 -Wall
7
hello
Exception: Stack<>::pop(): empty stack
$ clang++ Te_ba_stack1test.cpp -std=c++11 -Wall
7
hello
Exception: Stack<>::pop(): empty stack
$ clang++ Te_ba_stack1test.cpp -std=c++17 -Wall
7
hello
Exception: Stack<>::pop(): empty stack

$ g++-7 Te_ba_stack1test.cpp -std=c++03 -Wall
7
hello
Exception: Stack<>::pop(): empty stack

$ g++-7 Te_ba_stack1test.cpp -std=c++11 -Wall
7
hello
Exception: Stack<>::pop(): empty stack

$ g++-7 Te_ba_stack1test.cpp -std=c++17 -Wall
7
hello
Exception: Stack<>::pop(): empty stack


検討事項(agenda)

動作確認:push1回でpop2回。例外が発生し、異常終了。

どのstackで例外発生したかの表示があると良い。


参考資料(reference)


プログラミング言語教育のXYZ

https://qiita.com/kaizen_nagoya/items/1950c5810fb5c0b07be4

プログラミング言語教育のXYZ(youtube)

https://www.youtube.com/watch?v=He1_tg4px-w&t=486s


C++N4741 2018

Working Draft, Standard for Programming Language C++

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/n4741.pdf

C++N4741, 2018 Standard Working Draft on ISO/IEC 14882 sample code compile list

https://qiita.com/kaizen_nagoya/items/3294c014044550896010


C++N4606 2016

Working Draft, Standard for Programming Language C++

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/n4606.pdf

C++N4606, 2016符号断片編纂一覧(example code compile list)

Working Draft 2016, ISO/IEC 14882(1)

https://qiita.com/kaizen_nagoya/items/df5d62c35bd6ed1c3d43/


 CEDD(Compile Error Driven Design)

初めての CEDD(Compile Error Driven Design) 8回直してコンパイル。

https://qiita.com/kaizen_nagoya/items/9494236aa1753f3fd1e1

コンパイルエラーを記録するとよい理由7つ

https://qiita.com/kaizen_nagoya/items/85c0e92b206883140e89

N4606 Working Draft 2016, ISO/IEC 14882, C++ standardのコード断片をコンパイルするためにしていること

https://qiita.com/kaizen_nagoya/items/a8d7ee2f2e29e76c19c1

コンパイル用shell script C版(clangとgcc)とC++版(clang++とg++)

https://qiita.com/kaizen_nagoya/items/74220c0577a512c2d7da

Clang/Clang++(LLVM) gcc/g++(GNU) コンパイラ警告等比較

https://qiita.com/kaizen_nagoya/items/9a82b958cc3aeef0403f

Qiitaに投稿するCのStyle例(暫定)

https://qiita.com/kaizen_nagoya/items/946df1528a6a1ef2bc0d

MISRA C++ 5-0-16

https://qiita.com/kaizen_nagoya/items/7df2d4e05db724752a74

C++ Templates Part1 BASICS Chapter 3. Class Templates 3.2 Use of Class Template Stack stack1test.cpp

https://qiita.com/kaizen_nagoya/items/cd5fc49106fad5a4e9ed

ISO/IEC TS 17961:2013 C Secure Coding Rules(1) All list(to be confirmed)

https://qiita.com/kaizen_nagoya/items/54e056195c4f11b850a1

C言語(C++)に対する誤解、曲解、無理解、爽快。

https://qiita.com/kaizen_nagoya/items/3f3992c9722c1cee2e3a

C Puzzle Bookの有り難み5つ、C言語規格及びCコンパイラの特性を認識

https://qiita.com/kaizen_nagoya/items/d89a48c1536a02ecdec9

'wchar.h' file not found で困った clang++ macOS

https://qiita.com/kaizen_nagoya/items/de15cd46d657517fac11

Open POSIX Test Suiteの使い方を調べはじめました

https://qiita.com/kaizen_nagoya/items/644d5e407f5faf96e6dc

MISRA-C 2012 Referenceに掲載している文献の入手可能性を確認

https://qiita.com/kaizen_nagoya/items/96dc8b125e462d5575bb

どうやって MISRA Example Suiteをコンパイルするか

https://qiita.com/kaizen_nagoya/items/fbdbff5ff696e2ca7f00

MISRA C まとめ #include

https://qiita.com/kaizen_nagoya/items/f1a79a7cbd281607c7c9

「C++完全理解ガイド」の同意できること上位10

https://qiita.com/kaizen_nagoya/items/aa5744e0c4a8618c7671


文書履歴(document history)

ver 0.10 初稿 20180322

ver 0.11 stack1.hpp直接表示。return EXIT_SUCCESS。 -std=c++03,-std=c++11 20180429

ver 0.12 参考文献追記 20180616