LoginSignup
2
2
この記事誰得? 私しか得しないニッチな技術で記事投稿!

C++ Templates The Complete Guide(2nd Edition) (1) basics/errornovel1.cpp

Last updated at Posted at 2018-05-14

目的

このコンパイル記録は、コンパイラおよび対応標準、コンパイラの版により、コンパイルエラーMessageの違いを記録し、どのエラーが出たら、どの版またはどの対応標準にすればエラーが少なくなるかを考察するための資料です。

成果

コンパイラの種類、対応標準の違いによってエラーの数が違う。同じエラーであってもMessageの表現が違う。Error Messageのネットで検索する際に役立つ。

#背景
C++のTemplate使いこなせてない。
The Complete Guide(2nd Edition)
http://www.tmplbook.com/code/code.html
コンパイルしながら勉強。
勉強の記録
https://researchmap.jp/joqzln3gi-1797580/#_1797580
だと, qiitaのようにコード表示機能がないためわかりにくい。

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

コンパイルエラーになったものを
clang++, g++の -std=c++03, -std=c++2a
でコンパイルしながら一つづつ確認。

###clang++
clang version 6.0.0 (tags/RELEASE_600/final)
Target: x86_64-apple-darwin17.5.0
Thread model: posix
InstalledDir: /usr/local/opt/llvm/bin

###g++
g++ (Homebrew GCC 8.1.0) 8.1.0
Copyright (C) 2018 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.

basics/errornovel1.cpp
#include <string>
#include <map>
#include <algorithm>

int main()
{
  std::map<std::string,double> coll;
  //...
  // find the first nonempty string in coll:
  auto pos = std::find_if (coll.begin(), coll.end(),
                           [] (std::string const& s) {
                             return s != "";
                           });
}
cpla.sh
$ ../cpla.sh errornovel1
../cpla.sh: line 1: spla.sh: command not found
$ clang++ errornovel1.cpp -std=c++03 -Wall
errornovel1.cpp:10:3: warning: 'auto' type specifier is a C++11 extension
      [-Wc++11-extensions]
  auto pos = std::find_if (coll.begin(), coll.end(),
  ^
errornovel1.cpp:11:28: error: expected expression
                           [] (std::string const& s) { 
                           ^
1 warning and 1 error generated.
$ clang++ errornovel1.cpp -std=c++2a -Wall
errornovel1.cpp:10:8: warning: unused variable 'pos' [-Wunused-variable]
  auto pos = std::find_if (coll.begin(), coll.end(),
       ^
In file included from errornovel1.cpp:1:
In file included from /usr/local/Cellar/llvm/6.0.0/include/c++/v1/string:477:
In file included from /usr/local/Cellar/llvm/6.0.0/include/c++/v1/string_view:176:
In file included from /usr/local/Cellar/llvm/6.0.0/include/c++/v1/__string:56:
/usr/local/Cellar/llvm/6.0.0/include/c++/v1/algorithm:1010:13: error: no
      matching function for call to object of type '(lambda at
      errornovel1.cpp:11:28)'
        if (__pred(*__first))
            ^~~~~~
errornovel1.cpp:10:19: note: in instantiation of function template
      specialization
      'std::__1::find_if<std::__1::__map_iterator<std::__1::__tree_iterator<std::__1::__value_type<std::__1::basic_string<char>,
      double>,
      std::__1::__tree_node<std::__1::__value_type<std::__1::basic_string<char>,
      double>, void *> *, long> >, (lambda at errornovel1.cpp:11:28)>' requested
      here
  auto pos = std::find_if (coll.begin(), coll.end(),
                  ^
errornovel1.cpp:11:28: note: candidate function not viable: no known conversion
      from
      'std::__1::__map_iterator<std::__1::__tree_iterator<std::__1::__value_type<std::__1::basic_string<char>,
      double>,
      std::__1::__tree_node<std::__1::__value_type<std::__1::basic_string<char>,
      double>, void *> *, long> >::value_type' (aka 'pair<const
      std::__1::basic_string<char>, double>') to 'const std::string' (aka 'const
      basic_string<char, char_traits<char>, allocator<char> >') for 1st argument
                           [] (std::string const& s) { 
                           ^
errornovel1.cpp:11:28: note: conversion candidate of type 'bool (*)(const
      std::string &)' (aka 'bool (*)(const basic_string<char, char_traits<char>,
      allocator<char> > &)')
1 warning and 1 error generated.
\r
$ g++-8 errornovel1.cpp -std=c++03  -Wall
errornovel1.cpp: In function 'int main()':
errornovel1.cpp:10:3: warning: 'auto' changes meaning in C++11; please remove it [-Wc++11-compat]
   auto pos = std::find_if (coll.begin(), coll.end(),
   ^~~~
   ----
errornovel1.cpp:10:8: error: 'pos' does not name a type
   auto pos = std::find_if (coll.begin(), coll.end(),
        ^~~
errornovel1.cpp:13:29: error: expected primary-expression before ')' token
                            });
                             ^
\r
$ g++-8 errornovel1.cpp -std=c++2a  -Wall
errornovel1.cpp: In function 'int main()':
errornovel1.cpp:10:8: warning: variable 'pos' set but not used [-Wunused-but-set-variable]
   auto pos = std::find_if (coll.begin(), coll.end(),
        ^~~
In file included from /usr/local/Cellar/gcc/8.1.0/include/c++/8.1.0/bits/stl_algobase.h:71,
                 from /usr/local/Cellar/gcc/8.1.0/include/c++/8.1.0/bits/char_traits.h:39,
                 from /usr/local/Cellar/gcc/8.1.0/include/c++/8.1.0/string:40,
                 from errornovel1.cpp:1:
/usr/local/Cellar/gcc/8.1.0/include/c++/8.1.0/bits/predefined_ops.h: In instantiation of 'bool __gnu_cxx::__ops::_Iter_pred<_Predicate>::operator()(_Iterator) [with _Iterator = std::_Rb_tree_iterator<std::pair<const std::__cxx11::basic_string<char>, double> >; _Predicate = main()::<lambda(const string&)>]':
/usr/local/Cellar/gcc/8.1.0/include/c++/8.1.0/bits/stl_algo.h:104:42:   required from '_InputIterator std::__find_if(_InputIterator, _InputIterator, _Predicate, std::input_iterator_tag) [with _InputIterator = std::_Rb_tree_iterator<std::pair<const std::__cxx11::basic_string<char>, double> >; _Predicate = __gnu_cxx::__ops::_Iter_pred<main()::<lambda(const string&)> >]'
/usr/local/Cellar/gcc/8.1.0/include/c++/8.1.0/bits/stl_algo.h:161:23:   required from '_Iterator std::__find_if(_Iterator, _Iterator, _Predicate) [with _Iterator = std::_Rb_tree_iterator<std::pair<const std::__cxx11::basic_string<char>, double> >; _Predicate = __gnu_cxx::__ops::_Iter_pred<main()::<lambda(const string&)> >]'
/usr/local/Cellar/gcc/8.1.0/include/c++/8.1.0/bits/stl_algo.h:3930:28:   required from '_IIter std::find_if(_IIter, _IIter, _Predicate) [with _IIter = std::_Rb_tree_iterator<std::pair<const std::__cxx11::basic_string<char>, double> >; _Predicate = main()::<lambda(const string&)>]'
errornovel1.cpp:13:29:   required from here
/usr/local/Cellar/gcc/8.1.0/include/c++/8.1.0/bits/predefined_ops.h:283:11: error: no match for call to '(main()::<lambda(const string&)>) (std::pair<const std::__cxx11::basic_string<char>, double>&)'
  { return bool(_M_pred(*__it)); }
           ^~~~~~~~~~~~~~~~~~~~
/usr/local/Cellar/gcc/8.1.0/include/c++/8.1.0/bits/predefined_ops.h:283:11: note: candidate: 'bool (*)(const string&)' {aka 'bool (*)(const std::__cxx11::basic_string<char>&)'} <conversion>
/usr/local/Cellar/gcc/8.1.0/include/c++/8.1.0/bits/predefined_ops.h:283:11: note:   candidate expects 2 arguments, 2 provided
errornovel1.cpp:11:52: note: candidate: 'main()::<lambda(const string&)>'
                            [] (std::string const& s) {
                                                    ^
errornovel1.cpp:11:52: note:   no known conversion for argument 1 from 'std::pair<const std::__cxx11::basic_string<char>, double>' to 'const string&' {aka 'const std::__cxx11::basic_string<char>&'}

#まとめ

cpp clang++(error & warning) g++(error & warning)
-std=c++03 -std=c++2a -std=c++03 -std=c++2a
errornovel1 1 & 1 1 & 1 2 & 1 1 & 1

##検討事項(agenda) coding C++N4741
1)コンパイルエラーをなくす修正方法
2)役に立つまたは意味のあるその他の出力

#参考資料(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

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

docker gnu(gcc/g++) and llvm(clang/clang++)
https://qiita.com/drafts/059874ea39c4de64c0f7

Autosar Guidelines C++14 example code compile list
https://qiita.com/kaizen_nagoya/items/8ccbf6675c3494d57a76

[C][C++]の国際規格案の例題をコンパイルするときの課題7つ。
https://qiita.com/kaizen_nagoya/items/5f4b155030259497c4de

C++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

C++2003とC++2017でコンパイルエラーになるならない事例集
https://qiita.com/kaizen_nagoya/items/a13ea3823441c430edff

Qiitaに投稿するCのStyle例(暫定)
https://qiita.com/kaizen_nagoya/items/946df1528a6a1ef2bc0d

cpprefjpのdecltypeをコンパイル試験
https://qiita.com/kaizen_nagoya/items/090909af702f0d5d8a67

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 初稿 20180513 コンパイルエラーが十分取れていない。
clang++でコンパイルエラー18、g++は5つ。
ver. 0.11 誤記訂正 20180514
ver. 0.12 目的、成果追記 20180515
ver. 0.13 参照URL AUTOSAR C++14 guideliene追記 20180614
ver. 0.14 参考文献追記 20180616

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

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

Thank you very much for reading to the last sentence.

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

2
2
1

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
2
2