C++ Templates The Complete Guide(2nd Edition) (6) bridge/forupto4.cpp

More than 1 year has passed since last update.


目的

このコンパイル記録は、コンパイラおよび対応標準、コンパイラの版により、コンパイルエラー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.


bridge/forupto4.cpp

#include "functionptr.hpp"

#include <vector>
#include <iostream>

void forUpTo(int n, FunctionPtr<void(int)> f)
{
for (int i = 0; i != n; ++i)
{
f(i); // call passed function f for i
}
}

void printInt(int i)
{
std::cout << i << ' ';
}

int main()
{
std::vector<int> values;

// insert values from 0 to 4:
forUpTo(5,
[&values](int i) {
values.push_back(i);
});

// print elements:
forUpTo(5,
printInt); // prints 0 1 2 3 4
std::cout << '\n';
}



cpla.sh

$ ../cpla.sh forupto4

../cpla.sh: line 1: spla.sh: command not found
$ clang++ forupto4.cpp -std=c++03 -Wall
In file included from forupto4.cpp:1:
./functionptr.hpp:6:30: warning: variadic templates are a C++11 extension
[-Wc++11-extensions]
template<typename R, typename... Args>
^
./functionptr.hpp:10:3: error: no template named 'FunctorBridge'
FunctorBridge<R, Args...>* bridge;
^
./functionptr.hpp:19:26: warning: rvalue references are a C++11 extension
[-Wc++11-extensions]
FunctionPtr(FunctionPtr&& other) : bridge(other.bridge) {
^
./functionptr.hpp:23:37: warning: rvalue references are a C++11 extension
[-Wc++11-extensions]
template<typename F> FunctionPtr(F&& f); // see functionptr-init.hpp
^
./functionptr.hpp:31:37: warning: rvalue references are a C++11 extension
[-Wc++11-extensions]
FunctionPtr& operator=(FunctionPtr&& other) {
^
./functionptr.hpp:38:48: warning: rvalue references are a C++11 extension
[-Wc++11-extensions]
template<typename F> FunctionPtr& operator=(F&& f) {
^
./functionptr.hpp:52:3: warning: explicit conversion functions are a C++11 extension
[-Wc++11-extensions]
explicit operator bool() const {
^~~~~~~~
./functionptr.hpp:13:26: error: use of undeclared identifier 'nullptr'
FunctionPtr() : bridge(nullptr) {
^
./functionptr.hpp:20:20: error: use of undeclared identifier 'nullptr'
other.bridge = nullptr;
^
./functionptr.hpp:34:20: error: use of undeclared identifier 'nullptr'
other.bridge = nullptr;
^
./functionptr.hpp:39:21: error: use of undeclared identifier 'std'
FunctionPtr tmp(std::forward<F>(f));
^
./functionptr.hpp:39:34: error: 'F' does not refer to a value
FunctionPtr tmp(std::forward<F>(f));
^
./functionptr.hpp:38:21: note: declared here
template<typename F> FunctionPtr& operator=(F&& f) {
^
./functionptr.hpp:50:5: error: use of undeclared identifier 'std'
std::swap(fp1.bridge, fp2.bridge);
^
./functionptr.hpp:53:22: error: use of undeclared identifier 'nullptr'
return bridge == nullptr;
^
forupto4.cpp:5:44: error: implicit instantiation of undefined template
'FunctionPtr<void (int)>'
void forUpTo(int n, FunctionPtr<void(int)> f)
^
./functionptr.hpp:3:7: note: template is declared here
class FunctionPtr;
^
forupto4.cpp:24:11: error: expected expression
[&values](int i) {
^
forupto4.cpp:29:3: error: no matching function for call to 'forUpTo'
forUpTo(5,
^~~~~~~
forupto4.cpp:5:6: note: candidate function not viable: no known conversion from
'void (int)' to 'FunctionPtr<void (int)>' for 2nd argument
void forUpTo(int n, FunctionPtr<void(int)> f)
^
6 warnings and 11 errors generated.
$ clang++ forupto4.cpp -std=c++2a -Wall
In file included from forupto4.cpp:1:
./functionptr.hpp:10:3: error: no template named 'FunctorBridge'
FunctorBridge<R, Args...>* bridge;
^
./functionptr.hpp:39:21: error: use of undeclared identifier 'std'
FunctionPtr tmp(std::forward<F>(f));
^
./functionptr.hpp:39:34: error: 'F' does not refer to a value
FunctionPtr tmp(std::forward<F>(f));
^
./functionptr.hpp:38:21: note: declared here
template<typename F> FunctionPtr& operator=(F&& f) {
^
./functionptr.hpp:50:5: error: use of undeclared identifier 'std'
std::swap(fp1.bridge, fp2.bridge);
^
forupto4.cpp:5:44: error: implicit instantiation of undefined template
'FunctionPtr<void (int)>'
void forUpTo(int n, FunctionPtr<void(int)> f)
^
./functionptr.hpp:3:7: note: template is declared here
class FunctionPtr;
^
forupto4.cpp:23:3: error: no matching function for call to 'forUpTo'
forUpTo(5,
^~~~~~~
forupto4.cpp:5:6: note: candidate function not viable: no known conversion from '(lambda
at forupto4.cpp:24:11)' to 'FunctionPtr<void (int)>' for 2nd argument
void forUpTo(int n, FunctionPtr<void(int)> f)
^
forupto4.cpp:29:3: error: no matching function for call to 'forUpTo'
forUpTo(5,
^~~~~~~
forupto4.cpp:5:6: note: candidate function not viable: no known conversion from
'void (int)' to 'FunctionPtr<void (int)>' for 2nd argument
void forUpTo(int n, FunctionPtr<void(int)> f)
^
7 errors generated.
\r
$ g++-8 forupto4.cpp -std=c++03 -Wall
In file included from forupto4.cpp:1:
functionptr.hpp:13:3: warning: identifier 'nullptr' is a keyword in C++11 [-Wc++11-compat]
FunctionPtr() : bridge(nullptr) {
^~~~~~~~~~~
In file included from forupto4.cpp:1:
functionptr.hpp:6:30: warning: variadic templates only available with -std=c++11 or -std=gnu++11
template<typename R, typename... Args>
^~~
functionptr.hpp:7:25: warning: variadic templates only available with -std=c++11 or -std=gnu++11
class FunctionPtr<R(Args...)>
^~~
functionptr.hpp:10:3: error: 'FunctorBridge' does not name a type
FunctorBridge<R, Args...>* bridge;
^~~~~~~~~~~~~
functionptr.hpp:19:26: error: expected ',' or '...' before '&&' token
FunctionPtr(FunctionPtr&& other) : bridge(other.bridge) {
^~
functionptr.hpp:19:34: error: invalid constructor; you probably meant 'FunctionPtr<R(Args ...)> (const FunctionPtr<R(Args ...)>&)'
FunctionPtr(FunctionPtr&& other) : bridge(other.bridge) {
^
functionptr.hpp:23:37: error: expected ',' or '...' before '&&' token
template<typename F> FunctionPtr(F&& f); // see functionptr-init.hpp
^~
functionptr.hpp:31:37: error: expected ',' or '...' before '&&' token
FunctionPtr& operator=(FunctionPtr&& other) {
^~
functionptr.hpp:38:48: error: expected ',' or '...' before '&&' token
template<typename F> FunctionPtr& operator=(F&& f) {
^~
functionptr.hpp:52:28: warning: explicit conversion operators only available with -std=c++11 or -std=gnu++11
explicit operator bool() const {
^~~~~
functionptr.hpp:57:24: warning: variadic templates only available with -std=c++11 or -std=gnu++11
R operator()(Args... args) const; // see functionptr-cpinv.hpp
^~~~
functionptr.hpp: In constructor 'FunctionPtr<R(Args ...)>::FunctionPtr()':
functionptr.hpp:13:19: error: class 'FunctionPtr<R(Args ...)>' does not have any field named 'bridge'
FunctionPtr() : bridge(nullptr) {
^~~~~~
functionptr.hpp:13:26: error: 'nullptr' was not declared in this scope
FunctionPtr() : bridge(nullptr) {
^~~~~~~
functionptr.hpp: In copy constructor 'FunctionPtr<R(Args ...)>::FunctionPtr(FunctionPtr<R(Args ...)>&)':
functionptr.hpp:17:57: warning: delegating constructors only available with -std=c++11 or -std=gnu++11
: FunctionPtr(static_cast<FunctionPtr const&>(other)) {
^
functionptr.hpp: In member function 'FunctionPtr<R(Args ...)>& FunctionPtr<R(Args ...)>::operator=(FunctionPtr<R(Args ...)>)':
functionptr.hpp:32:12: error: 'bridge' was not declared in this scope
delete bridge;
^~~~~~
functionptr.hpp:33:14: error: 'other' was not declared in this scope
bridge = other.bridge;
^~~~~
functionptr.hpp:34:20: error: 'nullptr' was not declared in this scope
other.bridge = nullptr;
^~~~~~~
functionptr.hpp: In member function 'FunctionPtr<R(Args ...)>& FunctionPtr<R(Args ...)>::operator=(F)':
functionptr.hpp:39:26: error: 'forward' is not a member of 'std'
FunctionPtr tmp(std::forward<F>(f));
^~~~~~~
functionptr.hpp:39:26: note: 'std::forward' is only available from C++11 onwards
functionptr.hpp:39:35: error: expected primary-expression before '>' token
FunctionPtr tmp(std::forward<F>(f));
^
functionptr.hpp:39:37: error: 'f' was not declared in this scope
FunctionPtr tmp(std::forward<F>(f));
^
functionptr.hpp: In destructor 'FunctionPtr<R(Args ...)>::~FunctionPtr()':
functionptr.hpp:46:12: error: 'bridge' was not declared in this scope
delete bridge;
^~~~~~
functionptr.hpp: In function 'void swap(FunctionPtr<R(Args ...)>&, FunctionPtr<R(Args ...)>&)':
functionptr.hpp:50:10: error: 'swap' is not a member of 'std'
std::swap(fp1.bridge, fp2.bridge);
^~~~
functionptr.hpp: In member function 'FunctionPtr<R(Args ...)>::operator bool() const':
functionptr.hpp:53:12: error: 'bridge' was not declared in this scope
return bridge == nullptr;
^~~~~~
functionptr.hpp:53:22: error: 'nullptr' was not declared in this scope
return bridge == nullptr;
^~~~~~~
forupto4.cpp: In function 'int main()':
forupto4.cpp:26:11: warning: lambda expressions only available with -std=c++11 or -std=gnu++11
});
^
forupto4.cpp:26:12: error: could not convert '<lambda closure object>main()::<lambda(int)>{values}' from 'main()::<lambda(int)>' to 'FunctionPtr<void(int)>'
});
^
\r
$ g++-8 forupto4.cpp -std=c++2a -Wall
In file included from forupto4.cpp:1:
functionptr.hpp:10:3: error: 'FunctorBridge' does not name a type
FunctorBridge<R, Args...>* bridge;
^~~~~~~~~~~~~
functionptr.hpp: In constructor 'FunctionPtr<R(Args ...)>::FunctionPtr()':
functionptr.hpp:13:19: error: class 'FunctionPtr<R(Args ...)>' does not have any field named 'bridge'
FunctionPtr() : bridge(nullptr) {
^~~~~~
functionptr.hpp: In constructor 'FunctionPtr<R(Args ...)>::FunctionPtr(FunctionPtr<R(Args ...)>&&)':
functionptr.hpp:19:38: error: class 'FunctionPtr<R(Args ...)>' does not have any field named 'bridge'
FunctionPtr(FunctionPtr&& other) : bridge(other.bridge) {
^~~~~~
functionptr.hpp: In member function 'FunctionPtr<R(Args ...)>& FunctionPtr<R(Args ...)>::operator=(FunctionPtr<R(Args ...)>&&)':
functionptr.hpp:32:12: error: 'bridge' was not declared in this scope
delete bridge;
^~~~~~
functionptr.hpp: In member function 'FunctionPtr<R(Args ...)>& FunctionPtr<R(Args ...)>::operator=(F&&)':
functionptr.hpp:39:26: error: 'forward' is not a member of 'std'
FunctionPtr tmp(std::forward<F>(f));
^~~~~~~
functionptr.hpp:39:26: note: 'std::forward' is defined in header '<utility>'; did you forget to '#include <utility>'?
functionptr.hpp:1:1:
+#include <utility>
// primary template:
functionptr.hpp:39:26:
FunctionPtr tmp(std::forward<F>(f));
^~~~~~~
functionptr.hpp:39:35: error: expected primary-expression before '>' token
FunctionPtr tmp(std::forward<F>(f));
^
functionptr.hpp: In destructor 'FunctionPtr<R(Args ...)>::~FunctionPtr()':
functionptr.hpp:46:12: error: 'bridge' was not declared in this scope
delete bridge;
^~~~~~
functionptr.hpp: In function 'void swap(FunctionPtr<R(Args ...)>&, FunctionPtr<R(Args ...)>&)':
functionptr.hpp:50:10: error: 'swap' is not a member of 'std'
std::swap(fp1.bridge, fp2.bridge);
^~~~
functionptr.hpp: In member function 'FunctionPtr<R(Args ...)>::operator bool() const':
functionptr.hpp:53:12: error: 'bridge' was not declared in this scope
return bridge == nullptr;
^~~~~~
functionptr.hpp: At global scope:
functionptr.hpp:23:24: error: 'FunctionPtr<R(Args ...)>::FunctionPtr(F&&) [with F = main()::<lambda(int)>; R = void; Args = {int}]', declared using local type 'main()::<lambda(int)>', is used but never defined [-fpermissive]
template<typename F> FunctionPtr(F&& f); // see functionptr-init.hpp
^~~~~~~~~~~



まとめ

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

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

wrapper
11 & 6
7 & 0
19
11


検討事項(agenda)

1)コンパイルエラーをなくす修正方法

g++の方がclang++よりエラーが多い。

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


参考資料(reference)

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