LoginSignup
0
0

More than 1 year has passed since last update.

C++ Templates (1)1.1.2 Using the Template, max.cpp

Last updated at Posted at 2018-04-29

C++ Templates by David Vandevoorde, Nicolai M Josuttis, ISBN 978-0321714121, Addison-Wesley Professiona
http://www.tmplbook.com

この資料は
C++ Templates Part1: The BasicsChapter 1. Function Templates 1.1 A First Look at Function Templates 1.1.2 Using the Template max.cpp
https://researchmap.jp/jor3dv7ko-1797580/#_1797580
のQiitaへの転載です。転載に当たって、-std=c++03, -std=c++11, -std=c++17。
Qiitaの方が、プログラムの表示の色分けなど見やすい。

C++ Templates The Complete Guide(2nd Edition)をclang++とg++でコンパイルしてみた
https://qiita.com/kaizen_nagoya/items/a7065ea839cb33793bdf

<この項は書きかけです。順次追記します。>

Part1 BASICS

Chapter 1. Function Templates

1.1 A First Look at Function Templates

1.1.2 Using the templates

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. notes
7. source code
8. Compile and Go

算譜(source code)

T_ba_max.cpp
// 1 filename: Te_ba_max.cpp
// ver 0.1 18(Mon), Aug, 2017

#include <iostream>
#include <string>

// 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++ Template Part1: The Basics.\"");
std::string rus ("\"Chapter 2. Function Templates 2.1 A First Look at Function Templates 2.1.2 Using the Template max.cpp \"");

// 3 compile and output mechanism:
// (c) Dr. OGAWA Kiyoshi, 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_max.cpp -o Te_ba_maxL
// 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++  -std=c++17  -Wall Te_ba_max.cpp  -o Te_ba_maxG
// 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. 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.

// 7. source code
//{T}
/* 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.
*/
// max.cpp
#include "max.hpp"

int main()
{
    int i = 42;
    std::cout << "max(7,i):   " << ::max(7,i) << std::endl;

    double f1 = 3.4;
    double f2 = -6.7;
    std::cout << "max(f1,f2): " << ::max(f1,f2) << std::endl;

    std::string s1 = "mathematics";
    std::string s2 = "math";
    std::cout << "max(s1,s2): " << ::max(s1,s2) << std::endl;

    std::cout << ru << std::endl << rus << std::endl;/// added
    return EXIT_SUCCESS; /// added
}
max.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.
 */
template <typename T>
inline T const& max (T const& a, T const& b)
{
    // if a < b then use b else use a
    return  a < b ? b : a;
}

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

cppall.sh
$ ../cppall.sh Te_ba_max
$ clang++ Te_ba_max.cpp -std=c++03 -stdlib=libc++ -Wall
max(7,i):   42
max(f1,f2): 3.4
max(s1,s2): mathematics
"C++ Template Part1: The Basics."
"Chapter 2. Function Templates 2.1 A First Look at Function Templates 2.1.2 Using the Template max.cpp "
$ clang++ Te_ba_max.cpp -std=c++11 -stdlib=libc++  -Wall
max(7,i):   42
max(f1,f2): 3.4
max(s1,s2): mathematics
"C++ Template Part1: The Basics."
"Chapter 2. Function Templates 2.1 A First Look at Function Templates 2.1.2 Using the Template max.cpp "
$ clang++ Te_ba_max.cpp -std=c++17 -stdlib=libc++  -Wall
max(7,i):   42
max(f1,f2): 3.4
max(s1,s2): mathematics
"C++ Template Part1: The Basics."
"Chapter 2. Function Templates 2.1 A First Look at Function Templates 2.1.2 Using the Template max.cpp "

$ g++-7 Te_ba_max.cpp -std=c++03  -Wall
max(7,i):   42
max(f1,f2): 3.4
max(s1,s2): mathematics
"C++ Template Part1: The Basics."
"Chapter 2. Function Templates 2.1 A First Look at Function Templates 2.1.2 Using the Template max.cpp "

$ g++-7 Te_ba_max.cpp -std=c++11  -Wall
max(7,i):   42
max(f1,f2): 3.4
max(s1,s2): mathematics
"C++ Template Part1: The Basics."
"Chapter 2. Function Templates 2.1 A First Look at Function Templates 2.1.2 Using the Template max.cpp "

$ g++-7 Te_ba_max.cpp -std=c++17  -Wall
max(7,i):   42
max(f1,f2): 3.4
max(s1,s2): mathematics
"C++ Template Part1: The Basics."
"Chapter 2. Function Templates 2.1 A First Look at Function Templates 2.1.2 Using the Template max.cpp "

検討事項(agenda)

動作確認。
Templateを使い、型に依存しない事項と型に依存した事項を洗い出しできるか。

参考資料(reference)

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 初稿 20180429
ver. 0.02 ありがとう追記 20230413

最後までおよみいただきありがとうございました。

いいね 💚、フォローをお願いします。

Thank you very much for reading to the last sentence.

Please press the like icon 💚 and follow me for your happy life.

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0