C++ Templates The Complete Guide(2nd Edition) (7) deduce/aliastemplate.cpp

More than 1 year has passed since last update.


目的

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


成果

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

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

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


背景

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.


deduce/aliastemplate.cpp

template<typename T, typename Cont>

class Stack;

template<typename T>
using DequeStack = Stack<T, std::deque<T>>;

template<typename T, typename Cont>
void f1(Stack<T, Cont>);

template<typename T>
void f2(DequeStack<T>);

template<typename T>
void f3(Stack<T, std::deque<T>); // equivalent to f2

void test(DequeStack<int> intStack)
{
f1(intStack); // OK: T deduced to int, Cont deduced to std::deque<int>
f2(intStack); // OK: T deduced to int
f3(intStack); // OK: T deduced to int
}



cpla.sh

$ ../cpla.sh aliastemplate

$ clang++ aliastemplate.cpp -std=c++03 -Wall
aliastemplate.cpp:5:20: warning: alias declarations are a C++11 extension
[-Wc++11-extensions]
using DequeStack = Stack<T, std::deque<T>>;
^
aliastemplate.cpp:5:29: error: use of undeclared identifier 'std'
using DequeStack = Stack<T, std::deque<T>>;
^
aliastemplate.cpp:5:40: error: 'T' does not refer to a value
using DequeStack = Stack<T, std::deque<T>>;
^
aliastemplate.cpp:4:19: note: declared here
template<typename T>
^
aliastemplate.cpp:5:43: error: expected expression
using DequeStack = Stack<T, std::deque<T>>;
^
aliastemplate.cpp:11:6: warning: variable templates are a C++14 extension
[-Wc++14-extensions]
void f2(DequeStack<T>);
^
aliastemplate.cpp:11:6: error: variable has incomplete type 'void'
aliastemplate.cpp:11:9: error: use of undeclared identifier 'DequeStack'
void f2(DequeStack<T>);
^
aliastemplate.cpp:11:20: error: 'T' does not refer to a value
void f2(DequeStack<T>);
^
aliastemplate.cpp:10:19: note: declared here
template<typename T>
^
aliastemplate.cpp:11:22: error: expected expression
void f2(DequeStack<T>);
^
aliastemplate.cpp:14:18: error: use of undeclared identifier 'std'
void f3(Stack<T, std::deque<T>); // equivalent to f2
^
aliastemplate.cpp:14:29: error: 'T' does not refer to a value
void f3(Stack<T, std::deque<T>); // equivalent to f2
^
aliastemplate.cpp:13:19: note: declared here
template<typename T>
^
aliastemplate.cpp:14:29: error: 'T' does not refer to a value
void f3(Stack<T, std::deque<T>); // equivalent to f2
^
aliastemplate.cpp:13:19: note: declared here
template<typename T>
^
aliastemplate.cpp:16:6: error: variable has incomplete type 'void'
void test(DequeStack<int> intStack)
^
aliastemplate.cpp:16:11: error: use of undeclared identifier 'DequeStack'
void test(DequeStack<int> intStack)
^
aliastemplate.cpp:16:25: error: expected '(' for function-style cast or type construction
void test(DequeStack<int> intStack)
~~~^
aliastemplate.cpp:16:27: error: use of undeclared identifier 'intStack'
void test(DequeStack<int> intStack)
^
aliastemplate.cpp:16:36: error: expected ';' after top level declarator
void test(DequeStack<int> intStack)
^
;
2 warnings and 15 errors generated.
$ clang++ aliastemplate.cpp -std=c++2a -Wall
aliastemplate.cpp:5:29: error: use of undeclared identifier 'std'
using DequeStack = Stack<T, std::deque<T>>;
^
aliastemplate.cpp:5:40: error: 'T' does not refer to a value
using DequeStack = Stack<T, std::deque<T>>;
^
aliastemplate.cpp:4:19: note: declared here
template<typename T>
^
aliastemplate.cpp:11:6: error: variable has incomplete type 'void'
void f2(DequeStack<T>);
^
aliastemplate.cpp:11:9: error: use of undeclared identifier 'DequeStack'
void f2(DequeStack<T>);
^
aliastemplate.cpp:11:20: error: 'T' does not refer to a value
void f2(DequeStack<T>);
^
aliastemplate.cpp:10:19: note: declared here
template<typename T>
^
aliastemplate.cpp:11:22: error: expected expression
void f2(DequeStack<T>);
^
aliastemplate.cpp:14:18: error: use of undeclared identifier 'std'
void f3(Stack<T, std::deque<T>); // equivalent to f2
^
aliastemplate.cpp:14:29: error: 'T' does not refer to a value
void f3(Stack<T, std::deque<T>); // equivalent to f2
^
aliastemplate.cpp:13:19: note: declared here
template<typename T>
^
aliastemplate.cpp:14:29: error: 'T' does not refer to a value
void f3(Stack<T, std::deque<T>); // equivalent to f2
^
aliastemplate.cpp:13:19: note: declared here
template<typename T>
^
aliastemplate.cpp:16:6: error: variable has incomplete type 'void'
void test(DequeStack<int> intStack)
^
aliastemplate.cpp:16:11: error: use of undeclared identifier 'DequeStack'
void test(DequeStack<int> intStack)
^
aliastemplate.cpp:16:25: error: expected '(' for function-style cast or type construction
void test(DequeStack<int> intStack)
~~~^
aliastemplate.cpp:16:27: error: use of undeclared identifier 'intStack'
void test(DequeStack<int> intStack)
^
aliastemplate.cpp:16:36: error: expected ';' after top level declarator
void test(DequeStack<int> intStack)
^
;
14 errors generated.
\r
$ g++-8 aliastemplate.cpp -std=c++03 -Wall
aliastemplate.cpp:5:1: error: expected unqualified-id before 'using'
using DequeStack = Stack<T, std::deque<T>>;
^~~~~
aliastemplate.cpp:11:9: error: variable or field 'f2' declared void
void f2(DequeStack<T>);
^~~~~~~~~~
aliastemplate.cpp:11:9: error: 'DequeStack' was not declared in this scope
aliastemplate.cpp:11:9: note: suggested alternative: 'Stack'
void f2(DequeStack<T>);
^~~~~~~~~~
Stack
aliastemplate.cpp:11:21: error: expected primary-expression before '>' token
void f2(DequeStack<T>);
^
aliastemplate.cpp:11:22: error: expected primary-expression before ')' token
void f2(DequeStack<T>);
^
aliastemplate.cpp:14:23: error: 'deque' is not a member of 'std'
void f3(Stack<T, std::deque<T>); // equivalent to f2
^~~~~
aliastemplate.cpp:14:23: note: 'std::deque' is defined in header '<deque>'; did you forget to '#include <deque>'?
aliastemplate.cpp:1:1:
+#include <deque>
template<typename T, typename Cont>
aliastemplate.cpp:14:23:
void f3(Stack<T, std::deque<T>); // equivalent to f2
^~~~~
aliastemplate.cpp:14:23: error: 'deque' is not a member of 'std'
aliastemplate.cpp:14:23: note: 'std::deque' is defined in header '<deque>'; did you forget to '#include <deque>'?
aliastemplate.cpp:14:30: error: template argument 2 is invalid
void f3(Stack<T, std::deque<T>); // equivalent to f2
^
aliastemplate.cpp:14:23: error: 'deque' is not a member of 'std'
void f3(Stack<T, std::deque<T>); // equivalent to f2
^~~~~
aliastemplate.cpp:14:23: note: 'std::deque' is defined in header '<deque>'; did you forget to '#include <deque>'?
aliastemplate.cpp:14:23: error: 'deque' is not a member of 'std'
aliastemplate.cpp:14:23: note: 'std::deque' is defined in header '<deque>'; did you forget to '#include <deque>'?
aliastemplate.cpp:14:30: error: template argument 2 is invalid
void f3(Stack<T, std::deque<T>); // equivalent to f2
^
aliastemplate.cpp:14:9: error: variable or field 'f3' declared void
void f3(Stack<T, std::deque<T>); // equivalent to f2
^~~~~
aliastemplate.cpp:14:23: error: 'deque' is not a member of 'std'
void f3(Stack<T, std::deque<T>); // equivalent to f2
^~~~~
aliastemplate.cpp:14:23: note: 'std::deque' is defined in header '<deque>'; did you forget to '#include <deque>'?
aliastemplate.cpp:14:23: error: 'deque' is not a member of 'std'
aliastemplate.cpp:14:23: note: 'std::deque' is defined in header '<deque>'; did you forget to '#include <deque>'?
aliastemplate.cpp:14:30: error: template argument 2 is invalid
void f3(Stack<T, std::deque<T>); // equivalent to f2
^
aliastemplate.cpp:16:11: error: variable or field 'test' declared void
void test(DequeStack<int> intStack)
^~~~~~~~~~
aliastemplate.cpp:16:11: error: 'DequeStack' was not declared in this scope
aliastemplate.cpp:16:11: note: suggested alternative: 'Stack'
void test(DequeStack<int> intStack)
^~~~~~~~~~
Stack
aliastemplate.cpp:16:22: error: expected primary-expression before 'int'
void test(DequeStack<int> intStack)
^~~
\r
$ g++-8 aliastemplate.cpp -std=c++2a -Wall
aliastemplate.cpp:5:34: error: 'deque' is not a member of 'std'
using DequeStack = Stack<T, std::deque<T>>;
^~~~~
aliastemplate.cpp:5:34: note: 'std::deque' is defined in header '<deque>'; did you forget to '#include <deque>'?
aliastemplate.cpp:1:1:
+#include <deque>
template<typename T, typename Cont>
aliastemplate.cpp:5:34:
using DequeStack = Stack<T, std::deque<T>>;
^~~~~
aliastemplate.cpp:5:40: error: template argument 2 is invalid
using DequeStack = Stack<T, std::deque<T>>;
^
aliastemplate.cpp:11:9: error: variable or field 'f2' declared void
void f2(DequeStack<T>);
^~~~~~~~~~
aliastemplate.cpp:11:9: error: 'DequeStack' was not declared in this scope
aliastemplate.cpp:11:9: note: suggested alternative: 'Stack'
void f2(DequeStack<T>);
^~~~~~~~~~
Stack
aliastemplate.cpp:11:21: error: expected primary-expression before '>' token
void f2(DequeStack<T>);
^
aliastemplate.cpp:11:22: error: expected primary-expression before ')' token
void f2(DequeStack<T>);
^
aliastemplate.cpp:14:23: error: 'deque' is not a member of 'std'
void f3(Stack<T, std::deque<T>); // equivalent to f2
^~~~~
aliastemplate.cpp:14:23: note: 'std::deque' is defined in header '<deque>'; did you forget to '#include <deque>'?
aliastemplate.cpp:14:30: error: template argument 2 is invalid
void f3(Stack<T, std::deque<T>); // equivalent to f2
^
aliastemplate.cpp:14:23: error: 'deque' is not a member of 'std'
void f3(Stack<T, std::deque<T>); // equivalent to f2
^~~~~
aliastemplate.cpp:14:23: note: 'std::deque' is defined in header '<deque>'; did you forget to '#include <deque>'?
aliastemplate.cpp:14:30: error: template argument 2 is invalid
void f3(Stack<T, std::deque<T>); // equivalent to f2
^
aliastemplate.cpp:14:23: error: 'deque' is not a member of 'std'
void f3(Stack<T, std::deque<T>); // equivalent to f2
^~~~~
aliastemplate.cpp:14:23: note: 'std::deque' is defined in header '<deque>'; did you forget to '#include <deque>'?
aliastemplate.cpp:14:30: error: template argument 2 is invalid
void f3(Stack<T, std::deque<T>); // equivalent to f2
^
aliastemplate.cpp:14:23: error: 'deque' is not a member of 'std'
void f3(Stack<T, std::deque<T>); // equivalent to f2
^~~~~
aliastemplate.cpp:14:23: note: 'std::deque' is defined in header '<deque>'; did you forget to '#include <deque>'?
aliastemplate.cpp:14:30: error: template argument 2 is invalid
void f3(Stack<T, std::deque<T>); // equivalent to f2
^
aliastemplate.cpp:14:9: error: invalid template-id
void f3(Stack<T, std::deque<T>); // equivalent to f2
^~~~~
aliastemplate.cpp:14:23: error: 'deque' is not a member of 'std'
void f3(Stack<T, std::deque<T>); // equivalent to f2
^~~~~
aliastemplate.cpp:14:23: note: 'std::deque' is defined in header '<deque>'; did you forget to '#include <deque>'?
aliastemplate.cpp:14:31: error: variable or field 'f3' declared void
void f3(Stack<T, std::deque<T>); // equivalent to f2
^
aliastemplate.cpp:14:31: error: missing template arguments before ')' token
aliastemplate.cpp:16:11: error: variable or field 'test' declared void
void test(DequeStack<int> intStack)
^~~~~~~~~~
aliastemplate.cpp:16:11: error: 'DequeStack' was not declared in this scope
aliastemplate.cpp:16:11: note: suggested alternative: 'Stack'
void test(DequeStack<int> intStack)
^~~~~~~~~~
Stack
aliastemplate.cpp:16:22: error: expected primary-expression before 'int'
void test(DequeStack<int> intStack)
^~~



まとめ

inherit
clang++(error &
warning)
g++(error )

cpp
=-std=c++03
=-std=c++2a
=-std=c++03
=-std=c++2a

wrapper
計算中
計算中
計算中
計算中


検討事項(agenda)

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

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 初稿 20180515 コンパイルエラーが取れていない。