LoginSignup
1
1

More than 5 years have passed since last update.

C++17 - The Complete Guide clang++とg++でコンパイルしてみた(1) lang

Last updated at Posted at 2018-06-16

C++17 - The Complete Guide by Nicolai M. Josuttis
http://www.josuttis.com/cppstd17/

C++17 - The Complete Guide clang++とg++でコンパイルしてみた(まとめ)
https://qiita.com/kaizen_nagoya/items/c000f307e642990781e1

目的(purpose)

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

成果(outcome)

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

編纂器(compiler)

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.

一覧(list) lang

clang++ g++
error warning link error execute error 出力備考 error warning link error execute error 出力備考 c/g
aggr14.cpp 1 意図的誤 1 1 意図的誤 警告の無有
alignednew11.cpp 1 1
alignednew17.cpp 出力無 出力無
alignednewincomp.cpp 1 出力無 1 出力無
evalexcept.cpp
factory.cpp 2 出力無 1 出力無 警告の数
hexfloat.cpp
inlinethreadlocal1.cpp foo() inlinethreadlocal2と一緒にコンパイル foo() inlinethreadlocal2と一緒にコンパイル
inlinethreadlocal2.cpp _main 同上 _main 同上
lambdathis.cpp
noexceptcalls.cpp 1 1
structbind1.cpp 3 警告の有無
structbind2.cpp 3 警告の有無
tracknew.cpp 1 1

編纂実行(Compile and go) lang

aggr14

aggr14
clang++ lang/aggr14.cpp -std=c++2a -Wall
lang/aggr14.cpp:16:14: error: base class 'Base' has private default constructor
  Derived d1{};    // ERROR since C++17
             ^
lang/aggr14.cpp:7:3: note: declared private here
  Base() {
  ^
1 error generated.
g++ lang/aggr14.cpp -std=c++2a -Wall
lang/aggr14.cpp: In function 'int main()':
lang/aggr14.cpp:16:14: error: 'Base::Base()' is private within this context
   Derived d1{};    // ERROR since C++17
              ^
lang/aggr14.cpp:7:3: note: declared private here
   Base() {
   ^~~~
lang/aggr14.cpp:16:11: warning: unused variable 'd1' [-Wunused-variable]
   Derived d1{};    // ERROR since C++17
           ^~

alignednew11

alignednew11
clang++ lang/alignednew11.cpp -std=c++2a -Wall
In file included from lang/alignednew11.cpp:2:
lang/alignednew11.hpp:7:10: fatal error: 'malloc.h' file not found
#include <malloc.h>  // for _aligned_malloc() or memalign()
         ^~~~~~~~~~
1 error generated.
g++ lang/alignednew11.cpp -std=c++2a -Wall
In file included from lang/alignednew11.cpp:2:
lang/alignednew11.hpp:7:10: fatal error: malloc.h: No such file or directory
 #include <malloc.h>  // for _aligned_malloc() or memalign()
          ^~~~~~~~~~
compilation terminated.

alignednew17

alignednew17
clang++ lang/alignednew17.cpp -std=c++2a -Wall
g++ lang/alignednew17.cpp -std=c++2a -Wall

alignednewincomp

alignednewincomp
clang++ lang/alignednewincomp.cpp -std=c++2a -Wall
lang/alignednewincomp.cpp:18:6: warning: unused variable 'p' [-Wunused-variable]
  S* p = new S;  // calls our operator new only before C++17
     ^
1 warning generated.
g++ lang/alignednewincomp.cpp -std=c++2a -Wall
lang/alignednewincomp.cpp: In function 'int main()':
lang/alignednewincomp.cpp:18:6: warning: unused variable 'p' [-Wunused-variable]
   S* p = new S;  // calls our operator new only before C++17
      ^

evalexcept

evalexcept
clang++ lang/evalexcept.cpp -std=c++2a -Wall
value: 7
value: 14
value: 21
value: 28
value: EXCEPTION: vector
g++ lang/evalexcept.cpp -std=c++2a -Wall
value: 7
value: 14
value: 21
value: 28
value: EXCEPTION: vector::_M_range_check: __n (which is 4) >= this->size() (which is 4)

factory

factory
clang++ lang/factory.cpp -std=c++2a -Wall
lang/factory.cpp:9:20: warning: unused variable 'ai' [-Wunused-variable]
  std::atomic<int> ai = create<std::atomic<int>>(42);
                   ^
lang/factory.cpp:7:7: warning: unused variable 'i' [-Wunused-variable]
  int i = create<int>(42);
      ^
2 warnings generated.
g++ lang/factory.cpp -std=c++2a -Wall
lang/factory.cpp: In function 'int main()':
lang/factory.cpp:7:7: warning: unused variable 'i' [-Wunused-variable]
   int i = create<int>(42);
       ^

hexfloat

hexfloat
clang++ lang/hexfloat.cpp -std=c++2a -Wall
dec:     16  hex: 0x1p+4
dec:     10  hex: 0x1.4p+3
dec:     40  hex: 0x1.4p+5
dec:      5  hex: 0x1.4p+2
dec:      5  hex: 0x1.4p+2
dec: 100000  hex: 0x1.86ap+16
dec: 100000  hex: 0x1.86ap+16
dec: 49.625  hex: 0x1.8dp+5
g++ lang/hexfloat.cpp -std=c++2a -Wall
dec:     16  hex: 0x1p+4
dec:     10  hex: 0x1.4p+3
dec:     40  hex: 0x1.4p+5
dec:      5  hex: 0x1.4p+2
dec:      5  hex: 0x1.4p+2
dec: 100000  hex: 0x1.86ap+16
dec: 100000  hex: 0x1.86ap+16
dec: 49.625  hex: 0x1.8dp+5

inlinethreadlocal1

inlinethreadlocal1
clang++ lang/inlinethreadlocal1.cpp -std=c++2a -Wall
Undefined symbols for architecture x86_64:
  "foo()", referenced from:
      _main in inlinethreadlocal1-6e3ff4.o
ld: symbol(s) not found for architecture x86_64
clang-6.0: error: linker command failed with exit code 1 (use -v to see invocation)
g++ lang/inlinethreadlocal1.cpp -std=c++2a -Wall
Undefined symbols for architecture x86_64:
  "foo()", referenced from:
      _main in cckxcA6w.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status

inlinethreadlocal2

inlinethreadlocal2
clang++ lang/inlinethreadlocal2.cpp -std=c++2a -Wall
Undefined symbols for architecture x86_64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang-6.0: error: linker command failed with exit code 1 (use -v to see invocation)
g++ lang/inlinethreadlocal2.cpp -std=c++2a -Wall
Undefined symbols for architecture x86_64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status

lambdathis

lambdathis
clang++ lang/lambdathis.cpp -std=c++2a -Wall
c1
g++ lang/lambdathis.cpp -std=c++2a -Wall
c1

noexceptcalls

noexceptcalls
clang++ lang/noexceptcalls.cpp -std=c++2a -Wall
lang/noexceptcalls.cpp:20:3: error: no matching function for call to 'call'
  call(f1, f2);  // ERROR since C++17
  ^~~~
lang/noexceptcalls.cpp:5:6: note: candidate template ignored: deduced conflicting types for parameter 'T' ('void (*)()' vs. 'void (*)() noexcept')
void call(T op1, T op2)
     ^
1 error generated.
g++ lang/noexceptcalls.cpp -std=c++2a -Wall
lang/noexceptcalls.cpp: In function 'int main()':
lang/noexceptcalls.cpp:20:14: error: no matching function for call to 'call(void (&)(), void (&)() noexcept)'
   call(f1, f2);  // ERROR since C++17
              ^
lang/noexceptcalls.cpp:5:6: note: candidate: 'template<class T> void call(T, T)'
 void call(T op1, T op2)
      ^~~~
lang/noexceptcalls.cpp:5:6: note:   template argument deduction/substitution failed:
lang/noexceptcalls.cpp:20:14: note:   deduced conflicting types for parameter 'T' ('void (*)()' and 'void (*)() noexcept')
   call(f1, f2);  // ERROR since C++17
              ^

structbind1

structbind1
clang++ lang/structbind1.cpp -std=c++2a -Wall
In file included from lang/structbind1.cpp:2:
lang/structbind1.hpp:7:1: warning: 'tuple_size' defined as a struct template here but previously declared as a class template [-Wmismatched-tags]
struct std::tuple_size<Customer> {
^
/usr/local/Cellar/llvm/6.0.0/include/c++/v1/__tuple:25:22: note: did you mean struct here?
template <class _Tp> class _LIBCPP_TEMPLATE_VIS tuple_size;
                     ^
In file included from lang/structbind1.cpp:2:
lang/structbind1.hpp:12:1: warning: 'tuple_element' defined as a struct template here but previously declared as a class template [-Wmismatched-tags]
struct std::tuple_element<2, Customer> {
^
/usr/local/Cellar/llvm/6.0.0/include/c++/v1/__tuple:57:34: note: did you mean struct here?
template <size_t _Ip, class _Tp> class _LIBCPP_TEMPLATE_VIS tuple_element;
                                 ^
In file included from lang/structbind1.cpp:2:
lang/structbind1.hpp:16:1: warning: 'tuple_element' defined as a struct template here but previously declared as a class template [-Wmismatched-tags]
struct std::tuple_element<Idx, Customer> {
^
/usr/local/Cellar/llvm/6.0.0/include/c++/v1/__tuple:57:34: note: did you mean struct here?
template <size_t _Ip, class _Tp> class _LIBCPP_TEMPLATE_VIS tuple_element;
                                 ^
3 warnings generated.
f/l/v:    Tim Starr 42
f/l/v:     Waters 52
c:        Tim Starr 42
s:        Tim
g++ lang/structbind1.cpp -std=c++2a -Wall
f/l/v:    Tim Starr 42
f/l/v:     Waters 52
c:        Tim Starr 42
s:        Tim

structbind2

structbind2
clang++ lang/structbind2.cpp -std=c++2a -Wall
In file included from lang/structbind2.cpp:2:
lang/structbind2.hpp:7:1: warning: 'tuple_size' defined as a struct template here but previously declared as a class template [-Wmismatched-tags]
struct std::tuple_size<Customer> {
^
/usr/local/Cellar/llvm/6.0.0/include/c++/v1/__tuple:25:22: note: did you mean struct here?
template <class _Tp> class _LIBCPP_TEMPLATE_VIS tuple_size;
                     ^
In file included from lang/structbind2.cpp:2:
lang/structbind2.hpp:12:1: warning: 'tuple_element' defined as a struct template here but previously declared as a class template [-Wmismatched-tags]
struct std::tuple_element<2, Customer> {
^
/usr/local/Cellar/llvm/6.0.0/include/c++/v1/__tuple:57:34: note: did you mean struct here?
template <size_t _Ip, class _Tp> class _LIBCPP_TEMPLATE_VIS tuple_element;
                                 ^
In file included from lang/structbind2.cpp:2:
lang/structbind2.hpp:16:1: warning: 'tuple_element' defined as a struct template here but previously declared as a class template [-Wmismatched-tags]
struct std::tuple_element<Idx, Customer> {
^
/usr/local/Cellar/llvm/6.0.0/include/c++/v1/__tuple:57:34: note: did you mean struct here?
template <size_t _Ip, class _Tp> class _LIBCPP_TEMPLATE_VIS tuple_element;
                                 ^
3 warnings generated.
f/l/v:    Tim Starr 42
f2/l2/v2: Ringo Starr 52
c:        Ringo Starr 52
s:        Tim
g++ lang/structbind2.cpp -std=c++2a -Wall
f/l/v:    Tim Starr 42
f2/l2/v2: Ringo Starr 52
c:        Ringo Starr 52
s:        Tim

tracknew

tracknew
clang++ lang/tracknew.cpp -std=c++2a -Wall
In file included from lang/tracknew.cpp:2:
lang/tracknew.hpp:42:16: error: no member named 'aligned_alloc' in namespace 'std'
      p = std::aligned_alloc(align, size);  // C++17 API
          ~~~~~^
1 error generated.
g++ lang/tracknew.cpp -std=c++2a -Wall
In file included from lang/tracknew.cpp:2:
lang/tracknew.hpp: In static member function 'static void* TrackNew::allocate(std::size_t, std::size_t, const char*)':
lang/tracknew.hpp:42:16: error: 'aligned_alloc' is not a member of 'std'
       p = std::aligned_alloc(align, size);  // C++17 API
                ^~~~~~~~~~~~~
lang/tracknew.hpp:42:16: note: suggested alternative: 'aligned_union'
       p = std::aligned_alloc(align, size);  // C++17 API
                ^~~~~~~~~~~~~
                aligned_union

検討事項(agenda)

1)コンパイルエラーをなくす修正方法
1.1 inlinethreadlocal1, inlinethreadlocal2
2つのファイルをコンパイル対象に

$ clang++ ./lang/inlinethreadlocal1.cpp ./lang/inlinethreadlocal2.cpp -std=c++2a -Wall
$ ./a.out
main() begin:
- gName: global
- tName: tls
- lName: local
main() later:
- gName: thread1 name
- tName: thread1 name
- lName: thread1 name
foo() begin:
- gName: thread1 name
- tName: tls
- lName: local
foo() end:
- gName: thread2 name
- tName: thread2 name
- lName: thread2 name
main() end:
- gName: thread2 name
- tName: thread1 name
- lName: thread1 name
$ g++ ./lang/inlinethreadlocal1.cpp ./lang/inlinethreadlocal2.cpp -std=c++2a -Wall
$ ./a.out
main() begin:
- gName: global
- tName: tls
- lName: local
main() later:
- gName: thread1 name
- tName: thread1 name
- lName: thread1 name
foo() begin:
- gName: thread1 name
- tName: tls
- lName: local
foo() end:
- gName: thread2 name
- tName: thread2 name
- lName: thread2 name
main() end:
- gName: thread2 name
- tName: thread1 name
- lName: thread1 name

2)役に立つまたは意味のあるその他の出力

台本(script)

毎回コンパイル条件を変えてコンパイルするのに、一気にコンパイルする方法

cga.sh
#!/bin/bash
while read line
do
  echo "clang++ $line -std=c++2a -Wall"
  clang++ $line -std=c++2a -Wall 
  if [  -e a.out ]; then
  ./a.out
  rm a.out
  fi
  echo "g++ $line -std=c++2a -Wall"
  g++ $line -std=c++2a -Wall 
  if [  -e a.out ]; then
  ./a.out
  rm a.out
  fi
done < ./$1
do.sh
#!/bin/bash
while read line
do
 ls ./$line/*.cpp > ./$line/$line
 ./cga.sh ./$line/$line > $line.log.txt &> $line.log.txt
done < ./$1

参考資料(reference)

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

https://qiita.com/kaizen_nagoya/items/1950c5810fb5c0b07be4
プログラミング言語教育のXYZ(youtube)
https://www.youtube.com/watch?v=He1_tg4px-w&t=486s

Autosar Guidelines C++14

Autosar Guidelines C++14 example code compile list(1-169)
https://qiita.com/kaizen_nagoya/items/8ccbf6675c3494d57a76

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
docker gnu(gcc/g++) and llvm(clang/clang++)
https://qiita.com/drafts/059874ea39c4de64c0f7

[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 初稿 20180616

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