LoginSignup
0
0

More than 5 years have passed since last update.

C++N4741(61)8.6 Constant expressions [expr.const]p127

Last updated at Posted at 2018-05-20

はじめに(Introduction)

C++N4741 Working Draft, Standard for Programming Language C++
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/n4741.pdf

C++N4741は、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種類以上、最近になって解決できたことがあります。それは、主に次の4つの情報に基づいています。

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

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

作業方針(sequence)

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

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

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

初めての CEDD(Compile Error Driven Design) 8回直してコンパイル。
https://qiita.com/kaizen_nagoya/items/9494236aa1753f3fd1e1

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

clang version 6.0.0 (tags/RELEASE_600/final)
Target: x86_64-apple-darwin17.4.0

g++-7 --version

g++-7 (Homebrew GCC 7.3.0_1) 7.3.0
Copyright (C) 2017 Free Software Foundation, Inc.

(61)8.6 Constant expressions [expr.const]p127

C++N4606, 2016 (53)5.20 Constant expressions [expr.const]p138
https://qiita.com/kaizen_nagoya/items/326e9dcf8d91e28e2fc8

C++N3242, 2011 (42) 5.19 Constant expressions
https://researchmap.jp/joyino5ic-1797580/#_1797580

p127.cpp

算譜(source code)

p127.cpp
// C++N4741 Committee Draft, Standard for Programming Language C++
// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/n741.pdf
const char* msg= "C++N4741(61)8.6 Constant expressions [expr.const]p127.cpp";
// Edited by Dr. Ogawa Kiyoshi. Compile procedure and results record.

#include <iostream>
#include <cstdlib>

using namespace std;

void g() {
const int n = 0;
[=] {
constexpr int i = n; // OK, n is not odr-used here
constexpr int j = *&n; // ill-formed, &n would be an odr-use of n
};
}

auto monad = [](auto v) { return [=] { return v; }; };
auto bind = [](auto m) {
return [=](auto fvm) { return fvm(m()); };
};
// OK to have captures to automatic objects created during constant expression evaluation.
static_assert(bind(monad(2))(monad)() == monad(2)());

int x; // not constant
struct A {
constexpr A(bool b) : m(b?42:x) { }
int m;
};
constexpr int v = A(true).m; // OK: constructor call initializes m with the value 42
constexpr int w = A(false).m; // error: initializer for m is x, which is non-constant
constexpr int f1(int k) {
constexpr int x = k; // error: x is not initialized by a constant expression
// because lifetime of k began outside the initializer of x
return x;
}
constexpr int f2(int k) {
int x = k; // OK: not required to be a constant expression
// because x is not constexpr
return x;
}
constexpr int incr(int &n) {
return ++n;
}
constexpr int g(int k) {
constexpr int x = incr(k); // error: incr(k) is not a core constant expression
// because lifetime of k began outside the expression incr(k)
return x;
}
constexpr int h(int k) {
int x = incr(k); // OK: incr(k) is not required to be a core constant expression
return x;
}
constexpr int y = h(1); // OK: initializes y with the value 2
// h(1) is a core constant expression because
// the lifetime of k begins inside h(1)

struct A {
constexpr A(int i) : val(i) { }
constexpr operator int() const { return val; }
constexpr operator long() const { return 42; }
private:
int val;
};
template<int> struct X { };
constexpr A a = alignof(int);
alignas(a) int n; // error: ambiguous conversion
struct B { int n : a; }; // error: ambiguous conversion

bool f() {
char array[1 + int(1 + 0.2 - 0.1 - 0.1)]; // Must be evaluated during translation
int size = 1 + int(1 + 0.2 - 0.1 - 0.1); // May be evaluated at runtime
return sizeof(array) == size;
}

int main() {

  cout<< msg << endl;
  return EXIT_SUCCESS;
}

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

c1.sh
$ ../c1.sh p127
$ clang++ p127.cpp  -std=c++2a -Wall
p127.cpp:15:15: warning: unused variable 'j' [-Wunused-variable]
constexpr int j = *&n; // ill-formed, &n would be an odr-use of n
              ^
p127.cpp:14:15: warning: unused variable 'i' [-Wunused-variable]
constexpr int i = n; // OK, n is not odr-used here
              ^
p127.cpp:13:1: warning: expression result unused [-Wunused-value]
[=] {
^~~~~
p127.cpp:24:15: error: reference to 'bind' is ambiguous
static_assert(bind(monad(2))(monad)() == monad(2)());
              ^
p127.cpp:20:6: note: candidate found by name lookup is 'bind'
auto bind = [](auto m) {
     ^
/usr/local/Cellar/llvm/6.0.0/include/c++/v1/functional:2336:1: note: candidate found by name
      lookup is 'std::__1::bind'
bind(_Fp&& __f, _BoundArgs&&... __bound_args)
^
/usr/local/Cellar/llvm/6.0.0/include/c++/v1/functional:2327:1: note: candidate found by name
      lookup is 'std::__1::bind'
bind(_Fp&& __f, _BoundArgs&&... __bound_args)
^
p127.cpp:32:15: error: constexpr variable 'w' must be initialized by a constant expression
constexpr int w = A(false).m; // error: initializer for m is x, which is non-constant
              ^   ~~~~~~~~~~
p127.cpp:28:30: note: read of non-const variable 'x' is not allowed in a constant expression
constexpr A(bool b) : m(b?42:x) { }
                             ^
p127.cpp:32:19: note: in call to 'A(false)'
constexpr int w = A(false).m; // error: initializer for m is x, which is non-constant
                  ^
p127.cpp:26:5: note: declared here
int x; // not constant
    ^
p127.cpp:34:15: error: constexpr variable 'x' must be initialized by a constant expression
constexpr int x = k; // error: x is not initialized by a constant expression
              ^   ~
p127.cpp:34:19: note: read of non-const variable 'k' is not allowed in a constant expression
constexpr int x = k; // error: x is not initialized by a constant expression
                  ^
p127.cpp:33:22: note: declared here
constexpr int f1(int k) {
                     ^
p127.cpp:47:15: error: constexpr variable 'x' must be initialized by a constant expression
constexpr int x = incr(k); // error: incr(k) is not a core constant expression
              ^   ~~~~~~~
p127.cpp:44:8: note: a constant expression cannot modify an object that is visible outside that
      expression
return ++n;
       ^
p127.cpp:47:19: note: in call to 'incr(k)'
constexpr int x = incr(k); // error: incr(k) is not a core constant expression
                  ^
p127.cpp:69:9: error: ambiguous conversion from type 'const N::A' to an integral or unscoped
      enumeration type
alignas(a) int n; // error: ambiguous conversion
        ^
p127.cpp:62:11: note: conversion to integral type 'int' declared here
constexpr operator int() const { return val; }
          ^
p127.cpp:63:11: note: conversion to integral type 'long' declared here
constexpr operator long() const { return 42; }
          ^
p127.cpp:70:20: error: ambiguous conversion from type 'const N::A' to an integral or unscoped
      enumeration type
struct B { int n : a; }; // error: ambiguous conversion
                   ^
p127.cpp:62:11: note: conversion to integral type 'int' declared here
constexpr operator int() const { return val; }
          ^
p127.cpp:63:11: note: conversion to integral type 'long' declared here
constexpr operator long() const { return 42; }
          ^
3 warnings and 6 errors generated.

エラーを意図した行を///注釈に

算譜(source code)

p127a.cpp
// C++N4741 Committee Draft, Standard for Programming Language C++
// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/n741.pdf
const char* msg= "C++N4741(61)8.6 Constant expressions [expr.const]p127.cpp";
// Edited by Dr. Ogawa Kiyoshi. Compile procedure and results record.

#include <iostream>
#include <cstdlib>

using namespace std;

namespace N {
void g() {
  const int n = 0;
  [=] {
    constexpr int i = n; // OK, n is not odr-used here
    constexpr int j = *&n; // ill-formed, &n would be an odr-use of n
  };
}
}

auto monad = [](auto v) {
  return [=] { return v; };
};
auto bind = [](auto m) {
  return [=](auto fvm) {
    return fvm(m());
  };
};
// OK to have captures to automatic objects created during constant expression evaluation.
void h() {
  static_assert(bind(monad(2))(monad)() == monad(2)());
}

int x; // not constant
struct A {
  constexpr A(bool b) : m(b?42:x) { }
  int m;
};
constexpr int v = A(true).m; // OK: constructor call initializes m with the value 42
//constexpr int w = A(false).m; // error: initializer for m is x, which is non-constant
//constexpr int f1(int k) {
//constexpr int x = k; // error: x is not initialized by a constant expression
// because lifetime of k began outside the initializer of x
//constexpr int x = const_cast<int*>(&k);
//constexpr int x = reinterpret_cast<int>(k);
//return x;
//}
constexpr int f2(int k) {
  int x = k; // OK: not required to be a constant expression
// because x is not constexpr
  return x;
}
constexpr int incr(int &n) {
  return ++n;
}
//constexpr int g(int k) {
//constexpr int x = incr(k); // error: incr(k) is not a core constant expression
// because lifetime of k began outside the expression incr(k)
//return x;
//}
constexpr int h(int k) {
  int x = incr(k); // OK: incr(k) is not required to be a core constant expression
  return x;
}
constexpr int y = h(1); // OK: initializes y with the value 2
// h(1) is a core constant expression because
// the lifetime of k begins inside h(1)

namespace N {
struct A {
  constexpr A(int i) : val(i) { }
  constexpr operator int() const {
    return val;
  }
  constexpr operator long() const {
    return 42;
  }
private:
  int val;
};
template<int> struct X { };
constexpr A a = alignof(int);
//alignas(a) int n; // error: ambiguous conversion
//struct B { int n : a; }; // error: ambiguous conversion
}

bool f() {
  char array[1 + int(1 + 0.2 - 0.1 - 0.1)]; // Must be evaluated during translation
  int size = 1 + int(1 + 0.2 - 0.1 - 0.1); // May be evaluated at runtime
  return sizeof(array) == size;
}

int main() {
  N::g();
  h();
  cout << "f()="<<f()<< endl;
  cout<< msg << endl;
  return EXIT_SUCCESS;
}

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

c1.sh
$ ../c1.sh p127a
$ clang++ p127a.cpp  -std=c++2a -Wall
p127a.cpp:15:15: warning: unused variable 'i' [-Wunused-variable]
constexpr int i = n; // OK, n is not odr-used here
              ^
p127a.cpp:16:15: warning: unused variable 'j' [-Wunused-variable]
constexpr int j = *&n; // ill-formed, &n would be an odr-use of n
              ^
p127a.cpp:14:1: warning: expression result unused [-Wunused-value]
[=] {
^~~~~
p127a.cpp:27:16: error: reference to 'bind' is ambiguous
 static_assert(bind(monad(2))(monad)() == monad(2)());
               ^
p127a.cpp:22:6: note: candidate found by name lookup is 'bind'
auto bind = [](auto m) {
     ^
/usr/local/Cellar/llvm/6.0.0/include/c++/v1/functional:2336:1: note: candidate found by name
      lookup is 'std::__1::bind'
bind(_Fp&& __f, _BoundArgs&&... __bound_args)
^
/usr/local/Cellar/llvm/6.0.0/include/c++/v1/functional:2327:1: note: candidate found by name
      lookup is 'std::__1::bind'
bind(_Fp&& __f, _BoundArgs&&... __bound_args)
^
3 warnings and 1 error generated.

検討事項(agenda)

コンパイルエラーにならない修正方法。
役に立つまたは意味のあるその他の出力

参考資料(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 初稿 20180520
ver. 0.11 誤記訂正 20180523

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