LoginSignup
0
0

More than 1 year has passed since last update.

7.5.5.2 Closure types [expr.prim.lambda.closure] C++N4910:2022 (45) p105.cpp

Last updated at Posted at 2022-06-22

はじめに(Introduction)

N4910 Working Draft, Standard for Programming Language C++

n4910は、ISO/IEC JTC1 SC22 WG21の作業原案(Working Draft)です。
公式のISO/IEC 14882原本ではありません。
ISO/IEC JTC1 SC22 WG21では、可能な限り作業文書を公開し、幅広い意見を求めています。
一連の記事はコード断片をコンパイルできる形にする方法を検討してコンパイル、リンク、実行して、規格案の原文と処理系(g++, Clang++)との違いを確認し、技術内容を検討し、ISO/IEC JTC1 SC22 WG21にフィードバックするために用います。
また、CERT C++, MISRA C++等のコーディング標準のコード断片をコンパイルする際の参考にさせていただこうと考えています。CERT C++, MISRA C++が標準化の動きとの時間的なずれがあれば確認できれば幸いです。また、boostライブラリとの関連、Linux OS, TOPPERSカーネル、g++(GCC), clang++(LLVM)との関係も調査中です。
何か、抜け漏れ、耳より情報がありましたらおしらせくださると幸いです。

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

背景(back ground)

C/C++でコンパイルエラーが出ると、途方にくれることがしばしばあります。
何回かに1回は、該当するエラーが検索できます。
ただ、条件が違っていて、そこでの修正方法では目的を達成しないこともしばしばです。いろいろな条件のコンパイルエラーとその対応方法について、広く記録することによって、いつか同じエラーに遭遇した時にやくに立つことを目指しています。

この半年の間で、三度、自分のネットでの記録に助けられたことがあります。
また過去に解決できなかった記録を10種類以上、最近になって解決できたことがあります。それは、主に次の3つの情報に基づいています。

https://stackoverflow.com
https://cpprefjp.github.io
http://ja.cppreference.com/

また
https://researchmap.jp/joub9b3my-1797580/#_1797580
に記載したサイトのお世話になっています。

作業方針(sequence)

Clang++では-std=c++03, C++2bの2種類
g++では-std=c++03, c++2bの2種類
でコンパイルし、

1)コンパイルエラーを収集する。
2)コンパイルエラーをなくす方法を検討する。
コンパイルエラーになる例を示すだけが目的のコードは、コンパイルエラーをなくすのではなく、コンパイルエラーの種類を収集するだけにする。
文法を示すのが目的のコード場合に、コンパイルエラーをなくすのに手間がかかる場合は、順次作業します。
3)リンクエラーをなくす方法を検討する。
文法を示すのが目的のコード場合に、リンクエラーをなくすのに手間がかかる場合は、順次作業します。
4)意味のある出力を作る。
コンパイル、リンクが通っても、意味のある出力を示そうとすると、コンパイル・リンクエラーが出て収拾できそうにない場合がある。順次作業します。

1)だけのものから4)まで進んだものと色々ある状態です。一歩でも前に進むご助言をお待ちしています。「検討事項」の欄に現状を記録するようにしています。

C++N4910:2022 Standard Working Draft on ISO/IEC 14882(0) sample code compile list

C++N4741, 2018 Standard Working Draft on ISO/IEC 14882 sample code compile list

C++N4606, 2016符号断片編纂一覧(example code compile list)

C++N4606, 2016 Working Draft 2016, ISO/IEC 14882, C++ standard(1) Example code compile list
https://qiita.com/kaizen_nagoya/items/df5d62c35bd6ed1c3d43/

C++N3242, 2011 sample code compile list on clang++ and g++

編纂器(Compiler)

clang++ --version

Debian clang version 14.0.5-++20220610033153+c12386ae247c-1~exp1~20220610153237.151
Target: x86_64-pc-linux-gnu, Thread model: posix, InstalledDir: /usr/bin

g++- --version

g++ (GCC) 12.1.0 Copyright (C) 2022 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.

7.5.5.2 Closure types [expr.prim.lambda.closure] C++N4910:2022 (45) p105.cpp

算譜(source code)

p105.cpp
// C++N4910 Committee Draft, Standard for Programming Language C++
// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/n4910.pdf
const char * msg="7.5.5.2 Closure types [expr.prim.lambda.closure] C++N4910:2022 (45) p105.cpp";
// Debian clang version 14.0.5-++20220610033153+c12386ae247c-
// g++ (GCC) 12.1.0 Copyright (C) 2022 Free Software Foundation, Inc.
// Edited by Dr. Ogawa Kiyoshi. Compile procedure and results record.
// C++N4910:2022 Standard Working Draft on ISO/IEC 14882(0) sample code compile list
// https://qiita.com/kaizen_nagoya/items/fc957ddddd402004bb91

#include <iostream>
#include <cstdlib> 
#include <cstring>

using namespace std;

// Example 1
auto glambda = [](auto a, auto&& b) { return a < b; };
bool b = glambda(3, 3.14); // OK
auto vglambda = [](auto printer) {
return [=](auto&& ... ts) { // OK, ts is a function parameter pack
printer(std::forward<decltype(ts)>(ts)...);
return [=]() {
printer(ts ...);
};
};
};
auto p = vglambda( [](auto v1, auto v2, auto v3)
{ std::cout << v1 << v2 << v3; } );
auto q = p(1, 'a', 3.14); // OK, outputs 1a3.14
q(); // OK, outputs 1a3.14
auto fact = [](this auto self, int n) -> int { // OK, explicit object parameter
return (n <= 1) ? 1 : n * self(n-1);
};
std::cout << fact(5); // OK, outputs 120
// [Example 2 :
struct C {
template <typename T>
C(T);
};
void func(int i) {
int x = [=](this auto&&) { return i; }(); // OK
int y = [=](this C) { return i; }(); // error
int z = [](this C) { return 42; }(); // OK
}
// [Example 3 :
auto ID = [](auto a) { return a; };
static_assert(ID(3) == 3); // OK
struct NonLiteral {
NonLiteral(int n) : n(n) { }
int n;
};
static_assert(ID(NonLiteral{3}).n == 3); // error
// [Example 4 :
auto monoid = [](auto v) { return [=] { return v; }; };
auto add = [](auto m1) constexpr {
auto ret = m1();
return [=](auto m2) mutable {
auto m1val = m1();
auto plus = [=](auto m2val) mutable constexpr
{ return m1val += m2val; };
ret = plus(m2());
return monoid(ret);
};
};
constexpr auto zero = monoid(0);
constexpr auto one = monoid(1);
static_assert(add(one)(zero)() == one()); // OK
// Since two below is not declared constexpr, an evaluation of its constexpr member function call operator
// cannot perform an lvalue-to-rvalue conversion on one of its subobjects (that represents its capture)
// in a constant expression.
auto two = monoid(2);
assert(two() == 2); // OK, not a constant expression.
static_assert(add(one)(one)() == two()); // error: two() is not a constant expression
static_assert(add(one)(one)() == monoid(2)()); // OK
// [Example 5 :
template <typename T> concept C1 = /* ... */;
template <std::size_t N> concept C2 = /* ... */;
template <typename A, typename B> concept C3 = /* ... */;
auto f = []<typename T1, C1 T2> requires C2<sizeof(T1) + sizeof(T2)>
(T1 a1, T1 b1, T2 a2, auto a3, auto a4) requires C3<decltype(a4), T2> {
// T2 is constrained by a type-constraint.
// T1 and T2 are constrained by a requires-clause, and
// T2 and the type of a4 are constrained by a trailing requires-clause.
};
// Npte 4
auto glambda4 = [](auto a) { return a; };
int (*fp)(int) = glambda4;
// The behavior of the conversion function of glambda above is like that of the following conversion function:
struct Closure {
template<class T> auto operator()(T t) const { /* ... */ }
template<class T> static auto lambda_call_operator_invoker(T a) {
// forwards execution to operator()(a) and therefore has
// the same return type deduced
/* ... */
}
template<class T> using fptr_t =
decltype(lambda_call_operator_invoker(declval<T>())) (*)(T);
template<class T> operator fptr_t<T>() const
{ return &lambda_call_operator_invoker; }
};
// [Example 6 :
void f1(int (*)(int)) { }
void f2(char (*)(int)) { }
void g(int (*)(int)) { } // #1
void g(char (*)(char)) { } // #2
void h(int (*)(int)) { } // #3
void h(char (*)(int)) { } // #4
auto glambda6 = [](auto a) { return a; };
f1(glambda6); // OK
f2(glambda6); // error: ID is not convertible
g(glambda6); // error: ambiguous
h(glambda6); // OK, calls #3 since it is convertible from ID
int& (*fpi)(int*) = [](auto* a) -> auto& { return *a; }; // OK
// [Example 7 :
auto GL = [](auto a) { std::cout << a; return a; };
int (*GL_int)(int) = GL; // OK, through conversion function template
GL_int(3); // OK, same as GL(3)
// [Example 8 :
auto Fwd = [](int (*fp)(int), auto a) { return fp(a); };
auto C = [](auto a) { return a; };
static_assert(Fwd(C,3) == 3); // OK
// No specialization of the function call operator template can be constexpr (due to the local static).
auto NC = [](auto a) { static int s; return a; };
static_assert(Fwd(NC,3) == 3); // error
// [Example 9 :
struct S1 {
int x, y;
int operator()(int);
void f() {
[=]()->int {
return operator()(this->x + y); // equivalent to S1::operator()(this->x + (*this).y)
// this has type S1*
};
}
};
int main() {
  cout  <<  msg << endl;
  return EXIT_SUCCESS;
}

Script

clgc.sh
#!/bin/sh
rm $1l
rm $1g
echo "$ clang++ $1.cpp -std=03 -o $1l -I. -Wall" 
clang++ $1.cpp -std=c++03 -o $1l -I. -Wall
if [  -e $1l ]; then
./$1l 
fi
rm $1l
echo "$ clang++ $1.cpp -std=2b -o $1l -I. -Wall"
clang++ $1.cpp -std=c++2b -o $1l -I. -Wall
if [  -e $1l ]; then
./$1l
fi
echo "\r"
echo "$ g++ $1.cpp -std=03 -o $1g -I. -Wall"
g++ $1.cpp -std=c++03 -o $1g -I. -Wall
if [ -e $1g ]; then
./$1g
fi
rm $1g
echo "\r"
echo "$ g++ $1.cpp -std=2b -o $1g -I. -Wall"
g++ $1.cpp -std=c++2b -o $1g -I. -Wall
if [ -e $1g ]; then
./$1g 
fi

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

bash
# ./clgc.sh  p105
rm: cannot remove 'p105l': No such file or directory
rm: cannot remove 'p105g': No such file or directory
$ clang++ p105.cpp -std=03 -o p105l -I. -Wall
p105.cpp:17:1: warning: 'auto' type specifier is a C++11 extension [-Wc++11-extensions]
auto glambda = [](auto a, auto&& b) { return a < b; };
^
p105.cpp:17:16: error: expected expression
auto glambda = [](auto a, auto&& b) { return a < b; };
               ^
p105.cpp:19:1: warning: 'auto' type specifier is a C++11 extension [-Wc++11-extensions]
auto vglambda = [](auto printer) {
^
p105.cpp:19:17: error: expected expression
auto vglambda = [](auto printer) {
                ^
p105.cpp:27:1: warning: 'auto' type specifier is a C++11 extension [-Wc++11-extensions]
auto p = vglambda( [](auto v1, auto v2, auto v3)
^
p105.cpp:27:20: error: expected expression
auto p = vglambda( [](auto v1, auto v2, auto v3)
                   ^
p105.cpp:29:1: warning: 'auto' type specifier is a C++11 extension [-Wc++11-extensions]
auto q = p(1, 'a', 3.14); // OK, outputs 1a3.14
^
p105.cpp:30:1: error: C++ requires a type specifier for all declarations
q(); // OK, outputs 1a3.14
^
p105.cpp:31:1: warning: 'auto' type specifier is a C++11 extension [-Wc++11-extensions]
auto fact = [](this auto self, int n) -> int { // OK, explicit object parameter
^
p105.cpp:31:13: error: expected expression
auto fact = [](this auto self, int n) -> int { // OK, explicit object parameter
            ^
p105.cpp:34:6: error: no type named 'cout' in namespace 'std'
std::cout << fact(5); // OK, outputs 120
~~~~~^
p105.cpp:34:11: error: expected unqualified-id
std::cout << fact(5); // OK, outputs 120
          ^
p105.cpp:41:9: error: expected expression
int x = [=](this auto&&) { return i; }(); // OK
        ^
p105.cpp:42:9: error: expected expression
int y = [=](this C) { return i; }(); // error
        ^
p105.cpp:43:9: error: expected expression
int z = [](this C) { return 42; }(); // OK
        ^
p105.cpp:46:1: warning: 'auto' type specifier is a C++11 extension [-Wc++11-extensions]
auto ID = [](auto a) { return a; };
^
p105.cpp:46:11: error: expected expression
auto ID = [](auto a) { return a; };
          ^
p105.cpp:47:1: error: C++ requires a type specifier for all declarations
static_assert(ID(3) == 3); // OK
^
p105.cpp:52:1: error: C++ requires a type specifier for all declarations
static_assert(ID(NonLiteral{3}).n == 3); // error
^
p105.cpp:52:28: error: expected '(' for function-style cast or type construction
static_assert(ID(NonLiteral{3}).n == 3); // error
                 ~~~~~~~~~~^
p105.cpp:54:1: warning: 'auto' type specifier is a C++11 extension [-Wc++11-extensions]
auto monoid = [](auto v) { return [=] { return v; }; };
^
p105.cpp:54:15: error: expected expression
auto monoid = [](auto v) { return [=] { return v; }; };
              ^
p105.cpp:55:1: warning: 'auto' type specifier is a C++11 extension [-Wc++11-extensions]
auto add = [](auto m1) constexpr {
^
p105.cpp:55:12: error: expected expression
auto add = [](auto m1) constexpr {
           ^
p105.cpp:65:1: error: unknown type name 'constexpr'
constexpr auto zero = monoid(0);
^
p105.cpp:65:16: error: illegal storage class on file-scoped variable
constexpr auto zero = monoid(0);
               ^
p105.cpp:66:1: error: unknown type name 'constexpr'
constexpr auto one = monoid(1);
^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
8 warnings and 20 errors generated.
rm: cannot remove 'p105l': No such file or directory
$ clang++ p105.cpp -std=2b -o p105l -I. -Wall
p105.cpp:30:1: error: C++ requires a type specifier for all declarations
q(); // OK, outputs 1a3.14
^
p105.cpp:31:16: error: expected parameter declarator
auto fact = [](this auto self, int n) -> int { // OK, explicit object parameter
               ^
p105.cpp:31:16: error: expected ')'
p105.cpp:31:15: note: to match this '('
auto fact = [](this auto self, int n) -> int { // OK, explicit object parameter
              ^
p105.cpp:32:9: error: use of undeclared identifier 'n'
return (n <= 1) ? 1 : n * self(n-1);
        ^
p105.cpp:32:23: error: use of undeclared identifier 'n'
return (n <= 1) ? 1 : n * self(n-1);
                      ^
p105.cpp:32:32: error: use of undeclared identifier 'n'
return (n <= 1) ? 1 : n * self(n-1);
                               ^
p105.cpp:34:6: error: no type named 'cout' in namespace 'std'
std::cout << fact(5); // OK, outputs 120
~~~~~^
p105.cpp:34:11: error: expected unqualified-id
std::cout << fact(5); // OK, outputs 120
          ^
p105.cpp:41:13: error: expected parameter declarator
int x = [=](this auto&&) { return i; }(); // OK
            ^
p105.cpp:41:13: error: expected ')'
p105.cpp:41:12: note: to match this '('
int x = [=](this auto&&) { return i; }(); // OK
           ^
p105.cpp:42:13: error: expected parameter declarator
int y = [=](this C) { return i; }(); // error
            ^
p105.cpp:42:13: error: expected ')'
p105.cpp:42:12: note: to match this '('
int y = [=](this C) { return i; }(); // error
           ^
p105.cpp:43:12: error: expected parameter declarator
int z = [](this C) { return 42; }(); // OK
           ^
p105.cpp:43:12: error: expected ')'
p105.cpp:43:11: note: to match this '('
int z = [](this C) { return 42; }(); // OK
          ^
p105.cpp:52:15: error: static_assert expression is not an integral constant expression
static_assert(ID(NonLiteral{3}).n == 3); // error
              ^~~~~~~~~~~~~~~~~~~~~~~~
p105.cpp:52:15: note: non-literal type 'NonLiteral' cannot be used in a constant expression
p105.cpp:72:1: error: C++ requires a type specifier for all declarations
assert(two() == 2); // OK, not a constant expression.
^
p105.cpp:73:15: error: static_assert expression is not an integral constant expression
static_assert(add(one)(one)() == two()); // error: two() is not a constant expression
              ^~~~~~~~~~~~~~~~~~~~~~~~
p105.cpp:54:48: note: read of non-constexpr variable 'two' is not allowed in a constant expression
auto monoid = [](auto v) { return [=] { return v; }; };
                                               ^
p105.cpp:73:34: note: in call to '&two->operator()()'
static_assert(add(one)(one)() == two()); // error: two() is not a constant expression
                                 ^
p105.cpp:71:6: note: declared here
auto two = monoid(2);
     ^
p105.cpp:76:45: error: expected expression
template <typename T> concept C1 = /* ... */;
                                            ^
p105.cpp:77:48: error: expected expression
template <std::size_t N> concept C2 = /* ... */;
                                               ^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.

$ g++ p105.cpp -std=03 -o p105g -I. -Wall
p105.cpp:21:22: warning: identifier 'decltype' is a keyword in C++11 [-Wc++11-compat]
   21 | printer(std::forward<decltype(ts)>(ts)...);
      |                      ^~~~~~~~
p105.cpp:47:1: warning: identifier 'static_assert' is a keyword in C++11 [-Wc++11-compat]
   47 | static_assert(ID(3) == 3); // OK
      | ^~~~~~~~~~~~~
p105.cpp:55:24: warning: identifier 'constexpr' is a keyword in C++11 [-Wc++11-compat]
   55 | auto add = [](auto m1) constexpr {
      |                        ^~~~~~~~~
p105.cpp:17:1: warning: 'auto' changes meaning in C++11; please remove it [-Wc++11-compat]
   17 | auto glambda = [](auto a, auto&& b) { return a < b; };
      | ^~~~
      | ----
p105.cpp:17:6: error: 'glambda' does not name a type
   17 | auto glambda = [](auto a, auto&& b) { return a < b; };
      |      ^~~~~~~
p105.cpp:18:10: error: 'glambda' was not declared in this scope
   18 | bool b = glambda(3, 3.14); // OK
      |          ^~~~~~~
p105.cpp:19:1: warning: 'auto' changes meaning in C++11; please remove it [-Wc++11-compat]
   19 | auto vglambda = [](auto printer) {
      | ^~~~
      | ----
p105.cpp:19:6: error: 'vglambda' does not name a type
   19 | auto vglambda = [](auto printer) {
      |      ^~~~~~~~
p105.cpp:27:1: warning: 'auto' changes meaning in C++11; please remove it [-Wc++11-compat]
   27 | auto p = vglambda( [](auto v1, auto v2, auto v3)
      | ^~~~
      | ----
p105.cpp:27:6: error: 'p' does not name a type
   27 | auto p = vglambda( [](auto v1, auto v2, auto v3)
      |      ^
p105.cpp:28:34: error: expected unqualified-id before ')' token
   28 | { std::cout << v1 << v2 << v3; } );
      |                                  ^
p105.cpp:29:1: warning: 'auto' changes meaning in C++11; please remove it [-Wc++11-compat]
   29 | auto q = p(1, 'a', 3.14); // OK, outputs 1a3.14
      | ^~~~
      | ----
p105.cpp:29:6: error: 'q' does not name a type
   29 | auto q = p(1, 'a', 3.14); // OK, outputs 1a3.14
      |      ^
p105.cpp:30:4: error: expected constructor, destructor, or type conversion before ';' token
   30 | q(); // OK, outputs 1a3.14
      |    ^
p105.cpp:31:1: warning: 'auto' changes meaning in C++11; please remove it [-Wc++11-compat]
   31 | auto fact = [](this auto self, int n) -> int { // OK, explicit object parameter
      | ^~~~
      | ----
p105.cpp:31:6: error: 'fact' does not name a type
   31 | auto fact = [](this auto self, int n) -> int { // OK, explicit object parameter
      |      ^~~~
p105.cpp:34:6: error: 'cout' in namespace 'std' does not name a type
   34 | std::cout << fact(5); // OK, outputs 120
      |      ^~~~
In file included from p105.cpp:10:
/usr/local/include/c++/12.1.0/iostream:61:18: note: 'std::cout' declared here
   61 |   extern ostream cout;          /// Linked to standard output
      |                  ^~~~
p105.cpp: In function 'void func(int)':
p105.cpp:41:13: error: expected identifier before 'this'
   41 | int x = [=](this auto&&) { return i; }(); // OK
      |             ^~~~
p105.cpp:41:13: error: expected ',' or '...' before 'this'
p105.cpp:41:38: warning: lambda expressions only available with '-std=c++11' or '-std=gnu++11' [-Wc++11-extensions]
   41 | int x = [=](this auto&&) { return i; }(); // OK
      |                                      ^
p105.cpp:41:39: error: no match for call to '(func(int)::<lambda(int)>) ()'
   41 | int x = [=](this auto&&) { return i; }(); // OK
      |         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
p105.cpp:41:9: note: candidate: 'func(int)::<lambda(int)>'
   41 | int x = [=](this auto&&) { return i; }(); // OK
      |         ^
p105.cpp:41:9: note:   candidate expects 1 argument, 0 provided
p105.cpp:42:13: error: expected identifier before 'this'
   42 | int y = [=](this C) { return i; }(); // error
      |             ^~~~
p105.cpp:42:13: error: expected ',' or '...' before 'this'
p105.cpp:42:33: warning: lambda expressions only available with '-std=c++11' or '-std=gnu++11' [-Wc++11-extensions]
   42 | int y = [=](this C) { return i; }(); // error
      |                                 ^
p105.cpp:42:34: error: no match for call to '(func(int)::<lambda(int)>) ()'
   42 | int y = [=](this C) { return i; }(); // error
      |         ~~~~~~~~~~~~~~~~~~~~~~~~~^~
p105.cpp:42:9: note: candidate: 'func(int)::<lambda(int)>'
   42 | int y = [=](this C) { return i; }(); // error
      |         ^
p105.cpp:42:9: note:   candidate expects 1 argument, 0 provided
p105.cpp:43:12: error: expected identifier before 'this'
   43 | int z = [](this C) { return 42; }(); // OK
      |            ^~~~
p105.cpp:43:12: error: expected ',' or '...' before 'this'
p105.cpp:43:33: warning: lambda expressions only available with '-std=c++11' or '-std=gnu++11' [-Wc++11-extensions]
   43 | int z = [](this C) { return 42; }(); // OK
      |                                 ^
p105.cpp:43:34: error: no match for call to '(func(int)::<lambda(int)>) ()'
   43 | int z = [](this C) { return 42; }(); // OK
      |         ~~~~~~~~~~~~~~~~~~~~~~~~~^~
p105.cpp:43:34: note: candidate: 'int (*)(int)' (conversion)
p105.cpp:43:34: note:   candidate expects 2 arguments, 1 provided
p105.cpp:43:9: note: candidate: 'func(int)::<lambda(int)>'
   43 | int z = [](this C) { return 42; }(); // OK
      |         ^
p105.cpp:43:9: note:   candidate expects 1 argument, 0 provided
p105.cpp:41:5: warning: unused variable 'x' [-Wunused-variable]
   41 | int x = [=](this auto&&) { return i; }(); // OK
      |     ^
p105.cpp:42:5: warning: unused variable 'y' [-Wunused-variable]
   42 | int y = [=](this C) { return i; }(); // error
      |     ^
p105.cpp:43:5: warning: unused variable 'z' [-Wunused-variable]
   43 | int z = [](this C) { return 42; }(); // OK
      |     ^
p105.cpp: At global scope:
p105.cpp:46:1: warning: 'auto' changes meaning in C++11; please remove it [-Wc++11-compat]
   46 | auto ID = [](auto a) { return a; };
      | ^~~~
      | ----
p105.cpp:46:6: error: 'ID' does not name a type
   46 | auto ID = [](auto a) { return a; };
      |      ^~
p105.cpp:47:14: error: expected constructor, destructor, or type conversion before '(' token
   47 | static_assert(ID(3) == 3); // OK
      |              ^
p105.cpp:52:14: error: expected constructor, destructor, or type conversion before '(' token
   52 | static_assert(ID(NonLiteral{3}).n == 3); // error
      |              ^
p105.cpp:52:31: error: expected unqualified-id before ')' token
   52 | static_assert(ID(NonLiteral{3}).n == 3); // error
      |                               ^
p105.cpp:54:1: warning: 'auto' changes meaning in C++11; please remove it [-Wc++11-compat]
   54 | auto monoid = [](auto v) { return [=] { return v; }; };
      | ^~~~
      | ----
p105.cpp:54:6: error: 'monoid' does not name a type
   54 | auto monoid = [](auto v) { return [=] { return v; }; };
      |      ^~~~~~
p105.cpp:55:1: warning: 'auto' changes meaning in C++11; please remove it [-Wc++11-compat]
   55 | auto add = [](auto m1) constexpr {
      | ^~~~
      | ----
p105.cpp:55:6: error: 'add' does not name a type
   55 | auto add = [](auto m1) constexpr {
      |      ^~~
p105.cpp:65:1: error: 'constexpr' does not name a type
   65 | constexpr auto zero = monoid(0);
      | ^~~~~~~~~
p105.cpp:65:1: note: C++11 'constexpr' only available with '-std=c++11' or '-std=gnu++11'
p105.cpp:66:1: error: 'constexpr' does not name a type
   66 | constexpr auto one = monoid(1);
      | ^~~~~~~~~
p105.cpp:66:1: note: C++11 'constexpr' only available with '-std=c++11' or '-std=gnu++11'
p105.cpp:67:14: error: expected constructor, destructor, or type conversion before '(' token
   67 | static_assert(add(one)(zero)() == one()); // OK
      |              ^
p105.cpp:71:1: warning: 'auto' changes meaning in C++11; please remove it [-Wc++11-compat]
   71 | auto two = monoid(2);
      | ^~~~
      | ----
p105.cpp:71:6: error: 'two' does not name a type
   71 | auto two = monoid(2);
      |      ^~~
p105.cpp:72:7: error: expected constructor, destructor, or type conversion before '(' token
   72 | assert(two() == 2); // OK, not a constant expression.
      |       ^
p105.cpp:73:14: error: expected constructor, destructor, or type conversion before '(' token
   73 | static_assert(add(one)(one)() == two()); // error: two() is not a constant expression
      |              ^
p105.cpp:74:14: error: expected constructor, destructor, or type conversion before '(' token
   74 | static_assert(add(one)(one)() == monoid(2)()); // OK
      |              ^
p105.cpp:76:23: error: 'concept' does not name a type; did you mean 'const'?
   76 | template <typename T> concept C1 = /* ... */;
      |                       ^~~~~~~
      |                       const
p105.cpp:76:23: note: 'concept' only available with '-std=c++20' or '-fconcepts'
p105.cpp:77:26: error: 'concept' does not name a type; did you mean 'const'?
   77 | template <std::size_t N> concept C2 = /* ... */;
      |                          ^~~~~~~
      |                          const
p105.cpp:77:26: note: 'concept' only available with '-std=c++20' or '-fconcepts'
p105.cpp:78:35: error: 'concept' does not name a type; did you mean 'const'?
   78 | template <typename A, typename B> concept C3 = /* ... */;
      |                                   ^~~~~~~
      |                                   const
p105.cpp:78:35: note: 'concept' only available with '-std=c++20' or '-fconcepts'
p105.cpp:79:1: warning: 'auto' changes meaning in C++11; please remove it [-Wc++11-compat]
   79 | auto f = []<typename T1, C1 T2> requires C2<sizeof(T1) + sizeof(T2)>
      | ^~~~
      | ----
p105.cpp:79:6: error: 'f' does not name a type
   79 | auto f = []<typename T1, C1 T2> requires C2<sizeof(T1) + sizeof(T2)>
      |      ^
p105.cpp:86:1: warning: 'auto' changes meaning in C++11; please remove it [-Wc++11-compat]
   86 | auto glambda4 = [](auto a) { return a; };
      | ^~~~
      | ----
p105.cpp:86:6: error: 'glambda4' does not name a type
   86 | auto glambda4 = [](auto a) { return a; };
      |      ^~~~~~~~
p105.cpp:87:18: error: 'glambda4' was not declared in this scope
   87 | int (*fp)(int) = glambda4;
      |                  ^~~~~~~~
p105.cpp:90:19: warning: 'auto' changes meaning in C++11; please remove it [-Wc++11-compat]
   90 | template<class T> auto operator()(T t) const { /* ... */ }
      |                   ^~~~
      |                   ----
p105.cpp:90:24: error: ISO C++ forbids declaration of 'operator()' with no type [-fpermissive]
   90 | template<class T> auto operator()(T t) const { /* ... */ }
      |                        ^~~~~~~~
p105.cpp:90:19: error: storage class specified for 'operator()'
   90 | template<class T> auto operator()(T t) const { /* ... */ }
      |                   ^~~~
p105.cpp:91:26: warning: 'auto' changes meaning in C++11; please remove it [-Wc++11-compat]
   91 | template<class T> static auto lambda_call_operator_invoker(T a) {
      |                          ^~~~
      |                          ----
p105.cpp:91:19: error: conflicting specifiers in declaration of 'lambda_call_operator_invoker'
   91 | template<class T> static auto lambda_call_operator_invoker(T a) {
      |                   ^~~~~~
p105.cpp:96:19: error: expected unqualified-id before 'using'
   96 | template<class T> using fptr_t =
      |                   ^~~~~
p105.cpp:98:28: error: 'fptr_t' does not name a type
   98 | template<class T> operator fptr_t<T>() const
      |                            ^~~~~~
p105.cpp: In member function 'int Closure::operator()(T) const':
p105.cpp:90:58: warning: no return statement in function returning non-void [-Wreturn-type]
   90 | template<class T> auto operator()(T t) const { /* ... */ }
      |                                                          ^
p105.cpp: At global scope:
p105.cpp:108:1: warning: 'auto' changes meaning in C++11; please remove it [-Wc++11-compat]
  108 | auto glambda6 = [](auto a) { return a; };
      | ^~~~
      | ----
p105.cpp:108:6: error: 'glambda6' does not name a type
  108 | auto glambda6 = [](auto a) { return a; };
      |      ^~~~~~~~
p105.cpp:109:3: error: expected constructor, destructor, or type conversion before '(' token
  109 | f1(glambda6); // OK
      |   ^
p105.cpp:110:3: error: expected constructor, destructor, or type conversion before '(' token
  110 | f2(glambda6); // error: ID is not convertible
      |   ^
p105.cpp:111:2: error: expected constructor, destructor, or type conversion before '(' token
  111 | g(glambda6); // error: ambiguous
      |  ^
p105.cpp:112:2: error: expected constructor, destructor, or type conversion before '(' token
  112 | h(glambda6); // OK, calls #3 since it is convertible from ID
      |  ^
p105.cpp:113:24: warning: 'auto' changes meaning in C++11; please remove it [-Wc++11-compat]
  113 | int& (*fpi)(int*) = [](auto* a) -> auto& { return *a; }; // OK
      |                        ^~~~
      |                        ----
p105.cpp:113:30: error: ISO C++ forbids declaration of 'a' with no type [-fpermissive]
  113 | int& (*fpi)(int*) = [](auto* a) -> auto& { return *a; }; // OK
      |                              ^
p105.cpp:113:33: warning: C++11 auto only available with '-std=c++11' or '-std=gnu++11' [-Wc++11-extensions]
  113 | int& (*fpi)(int*) = [](auto* a) -> auto& { return *a; }; // OK
      |                                 ^~
p105.cpp:113:36: error: invalid use of 'auto'
  113 | int& (*fpi)(int*) = [](auto* a) -> auto& { return *a; }; // OK
      |                                    ^~~~
p105.cpp:113:55: warning: lambda expressions only available with '-std=c++11' or '-std=gnu++11' [-Wc++11-extensions]
  113 | int& (*fpi)(int*) = [](auto* a) -> auto& { return *a; }; // OK
      |                                                       ^
p105.cpp:115:1: warning: 'auto' changes meaning in C++11; please remove it [-Wc++11-compat]
  115 | auto GL = [](auto a) { std::cout << a; return a; };
      | ^~~~
      | ----
p105.cpp:115:6: error: 'GL' does not name a type
  115 | auto GL = [](auto a) { std::cout << a; return a; };
      |      ^~
p105.cpp:116:22: error: 'GL' was not declared in this scope
  116 | int (*GL_int)(int) = GL; // OK, through conversion function template
      |                      ^~
p105.cpp:117:7: error: expected constructor, destructor, or type conversion before '(' token
  117 | GL_int(3); // OK, same as GL(3)
      |       ^
p105.cpp:119:1: warning: 'auto' changes meaning in C++11; please remove it [-Wc++11-compat]
  119 | auto Fwd = [](int (*fp)(int), auto a) { return fp(a); };
      | ^~~~
      | ----
p105.cpp:119:6: error: 'Fwd' does not name a type
  119 | auto Fwd = [](int (*fp)(int), auto a) { return fp(a); };
      |      ^~~
p105.cpp:120:1: warning: 'auto' changes meaning in C++11; please remove it [-Wc++11-compat]
  120 | auto C = [](auto a) { return a; };
      | ^~~~
      | ----
p105.cpp:120:8: error: expected unqualified-id before '=' token
  120 | auto C = [](auto a) { return a; };
      |        ^
p105.cpp:121:14: error: expected constructor, destructor, or type conversion before '(' token
  121 | static_assert(Fwd(C,3) == 3); // OK
      |              ^
p105.cpp:123:1: warning: 'auto' changes meaning in C++11; please remove it [-Wc++11-compat]
  123 | auto NC = [](auto a) { static int s; return a; };
      | ^~~~
      | ----
p105.cpp:123:6: error: 'NC' does not name a type; did you mean 'C'?
  123 | auto NC = [](auto a) { static int s; return a; };
      |      ^~
      |      C
p105.cpp:124:14: error: expected constructor, destructor, or type conversion before '(' token
  124 | static_assert(Fwd(NC,3) == 3); // error
      |              ^
p105.cpp: In member function 'void S1::f()':
p105.cpp:133:1: warning: lambda expressions only available with '-std=c++11' or '-std=gnu++11' [-Wc++11-extensions]
  133 | };
      | ^
rm: cannot remove 'p105g': No such file or directory

$ g++ p105.cpp -std=2b -o p105g -I. -Wall
p105.cpp:30:4: error: expected constructor, destructor, or type conversion before ';' token
   30 | q(); // OK, outputs 1a3.14
      |    ^
p105.cpp:31:16: error: expected identifier before 'this'
   31 | auto fact = [](this auto self, int n) -> int { // OK, explicit object parameter
      |                ^~~~
p105.cpp:31:16: error: expected ',' or '...' before 'this'
p105.cpp: In lambda function:
p105.cpp:32:9: error: 'n' was not declared in this scope
   32 | return (n <= 1) ? 1 : n * self(n-1);
      |         ^
p105.cpp:32:27: error: 'self' was not declared in this scope
   32 | return (n <= 1) ? 1 : n * self(n-1);
      |                           ^~~~
p105.cpp: At global scope:
p105.cpp:34:6: error: 'cout' in namespace 'std' does not name a type
   34 | std::cout << fact(5); // OK, outputs 120
      |      ^~~~
In file included from p105.cpp:10:
/usr/local/include/c++/12.1.0/iostream:61:18: note: 'std::cout' declared here
   61 |   extern ostream cout;          /// Linked to standard output
      |                  ^~~~
p105.cpp: In function 'void func(int)':
p105.cpp:41:13: error: expected identifier before 'this'
   41 | int x = [=](this auto&&) { return i; }(); // OK
      |             ^~~~
p105.cpp:41:13: error: expected ',' or '...' before 'this'
p105.cpp:41:39: error: no match for call to '(func(int)::<lambda(int)>) ()'
   41 | int x = [=](this auto&&) { return i; }(); // OK
      |         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
p105.cpp:41:9: note: candidate: 'func(int)::<lambda(int)>'
   41 | int x = [=](this auto&&) { return i; }(); // OK
      |         ^
p105.cpp:41:9: note:   candidate expects 1 argument, 0 provided
p105.cpp:42:13: error: expected identifier before 'this'
   42 | int y = [=](this C) { return i; }(); // error
      |             ^~~~
p105.cpp:42:13: error: expected ',' or '...' before 'this'
p105.cpp:42:34: error: no match for call to '(func(int)::<lambda(int)>) ()'
   42 | int y = [=](this C) { return i; }(); // error
      |         ~~~~~~~~~~~~~~~~~~~~~~~~~^~
p105.cpp:42:9: note: candidate: 'func(int)::<lambda(int)>'
   42 | int y = [=](this C) { return i; }(); // error
      |         ^
p105.cpp:42:9: note:   candidate expects 1 argument, 0 provided
p105.cpp:43:12: error: expected identifier before 'this'
   43 | int z = [](this C) { return 42; }(); // OK
      |            ^~~~
p105.cpp:43:12: error: expected ',' or '...' before 'this'
p105.cpp:43:34: error: no match for call to '(func(int)::<lambda(int)>) ()'
   43 | int z = [](this C) { return 42; }(); // OK
      |         ~~~~~~~~~~~~~~~~~~~~~~~~~^~
p105.cpp:43:34: note: candidate: 'int (*)(int)' (conversion)
p105.cpp:43:34: note:   candidate expects 2 arguments, 1 provided
p105.cpp:43:9: note: candidate: 'func(int)::<lambda(int)>'
   43 | int z = [](this C) { return 42; }(); // OK
      |         ^
p105.cpp:43:9: note:   candidate expects 1 argument, 0 provided
p105.cpp:41:5: warning: unused variable 'x' [-Wunused-variable]
   41 | int x = [=](this auto&&) { return i; }(); // OK
      |     ^
p105.cpp:42:5: warning: unused variable 'y' [-Wunused-variable]
   42 | int y = [=](this C) { return i; }(); // error
      |     ^
p105.cpp:43:5: warning: unused variable 'z' [-Wunused-variable]
   43 | int z = [](this C) { return 42; }(); // OK
      |     ^
p105.cpp: At global scope:
p105.cpp:52:35: error: non-constant condition for static assertion
   52 | static_assert(ID(NonLiteral{3}).n == 3); // error
      |               ~~~~~~~~~~~~~~~~~~~~^~~~
p105.cpp:52:17: error: temporary of non-literal type 'NonLiteral' in a constant expression
   52 | static_assert(ID(NonLiteral{3}).n == 3); // error
      |               ~~^~~~~~~~~~~~~~~
p105.cpp:48:8: note: 'NonLiteral' is not literal because:
   48 | struct NonLiteral {
      |        ^~~~~~~~~~
p105.cpp:48:8: note:   'NonLiteral' is not an aggregate, does not have a trivial default constructor, and has no 'constexpr' constructor that is not a copy or move constructor
p105.cpp:72:7: error: expected constructor, destructor, or type conversion before '(' token
   72 | assert(two() == 2); // OK, not a constant expression.
      |       ^
p105.cpp:73:31: error: non-constant condition for static assertion
   73 | static_assert(add(one)(one)() == two()); // error: two() is not a constant expression
      |               ~~~~~~~~~~~~~~~~^~~~~~~~
p105.cpp:73:37:   in 'constexpr' expansion of 'two.<lambda(auto:19)>::<lambda()>()'
p105.cpp:73:31: error: the value of 'two' is not usable in a constant expression
p105.cpp:71:6: note: 'two' was not declared 'constexpr'
   71 | auto two = monoid(2);
      |      ^~~
p105.cpp:76:45: error: expected primary-expression before ';' token
   76 | template <typename T> concept C1 = /* ... */;
      |                                             ^
p105.cpp:77:48: error: expected primary-expression before ';' token
   77 | template <std::size_t N> concept C2 = /* ... */;
      |                                                ^
p105.cpp:78:57: error: expected primary-expression before ';' token
   78 | template <typename A, typename B> concept C3 = /* ... */;
      |                                                         ^
p105.cpp:109:3: error: expected constructor, destructor, or type conversion before '(' token
  109 | f1(glambda6); // OK
      |   ^
p105.cpp:110:3: error: expected constructor, destructor, or type conversion before '(' token
  110 | f2(glambda6); // error: ID is not convertible
      |   ^
p105.cpp:111:2: error: expected constructor, destructor, or type conversion before '(' token
  111 | g(glambda6); // error: ambiguous
      |  ^
p105.cpp:112:2: error: expected constructor, destructor, or type conversion before '(' token
  112 | h(glambda6); // OK, calls #3 since it is convertible from ID
      |  ^
p105.cpp:117:7: error: expected constructor, destructor, or type conversion before '(' token
  117 | GL_int(3); // OK, same as GL(3)
      |       ^
p105.cpp: In instantiation of '<lambda(auto:31)> [with auto:31 = int]':
p105.cpp:123:11:   required by substitution of 'template<class auto:31> constexpr<lambda(auto:31)>::operator decltype (((const<lambda(auto:31)>*)0)->operator()<auto:31>(static_cast<auto:31&&>(<anonymous>))) (*)(auto:31)() const [with auto:31 = int]'
p105.cpp:124:18:   required from here
p105.cpp:123:35: warning: unused variable 's' [-Wunused-variable]
  123 | auto NC = [](auto a) { static int s; return a; };
      |                                   ^
p105.cpp:124:25: error: non-constant condition for static assertion
  124 | static_assert(Fwd(NC,3) == 3); // error
      |               ~~~~~~~~~~^~~~
p105.cpp:124:18:   in 'constexpr' expansion of 'Fwd.<lambda(int (*)(int), auto:29)>(NC.<lambda(auto:31)>::operator int (*)(int)<int>(), 3)'
p105.cpp:119:50:   in 'constexpr' expansion of 'fp(a)'
p105.cpp:124:25: error: '<lambda(auto:31)> [with auto:31 = int]' called in a constant expression
p105.cpp:123:11: note: '<lambda(auto:31)> [with auto:31 = int]' is not usable as a 'constexpr' function because:
  123 | auto NC = [](auto a) { static int s; return a; };
      |           ^
p105.cpp:123:35: error: 's' defined 'static' in 'constexpr' context
  123 | auto NC = [](auto a) { static int s; return a; };
      |                                   ^
p105.cpp: In lambda function:
p105.cpp:130:1: warning: implicit capture of 'this' via '[=]' is deprecated in C++20 [-Wdeprecated]
  130 | [=]()->int {
      | ^
p105.cpp:130:1: note: add explicit 'this' or '*this' capture
ca

検討事項(agenda)

コンパイルエラーの理由を解説する。

<|--コンパイルエラーを取るか、コンパイルエラーの理由を解説する。-->

参考資料(reference)

typedef は C++11 ではオワコン

C99からC++14を駆け抜けるC++講座

自己参照(self reference)

C++ Error Message Collection(1)does not name a type, 11 articles

dockerにclang

docker gnu(gcc/g++) and llvm(clang/clang++)

コンパイル用shell script C版(clangとgcc)とC++版(clang++とg++)

Compare the contents of C++N4910:2022, C++N4741:2018 and C++N4606:2015

C++ sample list

clang++, g++コンパイルエラー方針の違いの例

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.01 初稿  20220622

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