回答編集履歴

3

m

2020/08/12 12:00

投稿

yumetodo
yumetodo

スコア5850

test CHANGED
@@ -473,3 +473,127 @@
473
473
  本件とは別の問題点からになりますが、これまでイテレータを作るときに継承するべきとされていた`std::iterator`はC++17でdeprecatedになりました。まあそんなこんなであんまり継承したくない心情です。
474
474
 
475
475
  [P0174R2 Deprecating Vestigial Library Parts in C++17](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0174r2.html)
476
+
477
+
478
+
479
+ ---
480
+
481
+
482
+
483
+ # 追記2
484
+
485
+
486
+
487
+ > 私の勝手な憶測なのですが
488
+
489
+ > もしかして
490
+
491
+ >
492
+
493
+ > 規格1:
494
+
495
+ > その規格で必要になる規格1
496
+
497
+ > その規格で必要になる規格2
498
+
499
+ >
500
+
501
+ > といった書き方がされているのでしょうか。
502
+
503
+
504
+
505
+
506
+
507
+ 違います。構文を定義する文法です。C++に限らず広く用いられる記法です。
508
+
509
+ まず定義したい構文の名称を書き、それを構成する構文や記号などの並びをその下に字下げして書きます。
510
+
511
+ 例えば識別子(identifier)の定義は
512
+
513
+ [https://timsong-cpp.github.io/cppwp/n4861/lex.name#nt:identifier](https://timsong-cpp.github.io/cppwp/n4861/lex.name#nt:identifier)
514
+
515
+ にあるように表されます。
516
+
517
+
518
+
519
+ ```
520
+
521
+ identifier:
522
+
523
+ identifier-nondigit
524
+
525
+ identifier identifier-nondigit
526
+
527
+ identifier digit
528
+
529
+ ```
530
+
531
+
532
+
533
+ これはつまり、`identifier`という構文は、その下の各行のいずれかのことである、という定義です。じゃあ`identifier-nondigit`ってなに?と思って調べるとこれは
534
+
535
+
536
+
537
+ ```
538
+
539
+ identifier-nondigit:
540
+
541
+ nondigit
542
+
543
+ universal-character-name
544
+
545
+ ```
546
+
547
+
548
+
549
+ のように定義されています。じゃあ`nondigit`は?というと
550
+
551
+
552
+
553
+ ```
554
+
555
+ nondigit: one of
556
+
557
+ a b c d e f g h i j k l m
558
+
559
+ n o p q r s t u v w x y z
560
+
561
+ A B C D E F G H I J K L M
562
+
563
+ N O P Q R S T U V W X Y Z _
564
+
565
+ ```
566
+
567
+
568
+
569
+ と定義されています。
570
+
571
+
572
+
573
+ ちょっと戻ってもう一度これを見てください。
574
+
575
+
576
+
577
+ ```
578
+
579
+ identifier:
580
+
581
+ identifier-nondigit
582
+
583
+ identifier identifier-nondigit
584
+
585
+ identifier digit
586
+
587
+ ```
588
+
589
+
590
+
591
+ `identifier identifier-nondigit`ってどういうこと?と思うかもしれません。なんで`identifier`の定義に`identifier`が出てくるねん!ってなりますよね。これはつまりは再帰的定義をしているわけです。
592
+
593
+
594
+
595
+ 例えば`a`は`nondigit`であり、`identifier-nondigit`でもあり、`identifier`でもあります。
596
+
597
+
598
+
599
+ では`ab`はどうでしょうか?まず`b`は同様にして`identifier-nondigit`であると言えます。`a`が`identifier`であることはすでに述べたとおりです。したがって`identifier identifier-nondigit`の定義に当てはまり、`ab`は`identifier`だと言えます(定義文はスペースで区切られていますが、このスペースは定義文に含まれる構文用語を識別しやすくするためのものであってスペースを入れろという意味ではありません。つまり`a b`ではなく`ab`ということです。定義にスペースを入れてほしい場合は明示的に`space`みたいな構文用語を記述するはずです)。

2

追記

2020/08/12 12:00

投稿

yumetodo
yumetodo

スコア5850

test CHANGED
@@ -263,3 +263,213 @@
263
263
 
264
264
 
265
265
  ちなみにC++20を使えるなら三方比較演算子を使うと楽になると思います。
266
+
267
+
268
+
269
+ ---
270
+
271
+
272
+
273
+ # 追記
274
+
275
+
276
+
277
+ ## なぜ派生クラスのコンストラクタのメンバー初期化リスト(`mem-initializer-list`)で基底クラスのメンバー変数を初期化できないか
278
+
279
+
280
+
281
+ ### `mem-initializer-list`とは
282
+
283
+
284
+
285
+ まずはC++規格書の定義を見てみましょう。
286
+
287
+
288
+
289
+ > [class.base.init/1](https://timsong-cpp.github.io/cppwp/n4861/class.base.init#1)
290
+
291
+ >
292
+
293
+ > In the definition of a constructor for a class, initializers for direct and virtual base class subobjects and non-static data members can be specified by a ctor-initializer, which has the form
294
+
295
+ >
296
+
297
+ > ```
298
+
299
+ > ctor-initializer:
300
+
301
+ > : mem-initializer-list
302
+
303
+ >
304
+
305
+ > mem-initializer-list:
306
+
307
+ > mem-initializer ...opt
308
+
309
+ > mem-initializer-list , mem-initializer ...opt
310
+
311
+ >
312
+
313
+ > mem-initializer:
314
+
315
+ > mem-initializer-id ( expression-listopt )
316
+
317
+ > mem-initializer-id braced-init-list
318
+
319
+ >
320
+
321
+ > mem-initializer-id:
322
+
323
+ > class-or-decltype
324
+
325
+ > identifier
326
+
327
+ > ```
328
+
329
+
330
+
331
+ 英文は読まずともまあ十分わかりやすいかと思いますが、例を見ながら確認しましょう。
332
+
333
+
334
+
335
+ ```cpp
336
+
337
+ struct foo {
338
+
339
+ int bar;
340
+
341
+ int hoge;
342
+
343
+ foo() : bar(3), hoge(7) {}
344
+
345
+ };
346
+
347
+ ```
348
+
349
+
350
+
351
+ 上記で言う`bar(3), hoge(7)`が`mem-initializer-list`です。また`bar(3)`や`hoge(7)`が`mem-initializer`です。そして`bar`や`hoge`が`mem-initializer-id`です。
352
+
353
+
354
+
355
+ ### 有効な`mem-initializer-id`とは
356
+
357
+
358
+
359
+ またしても規格書を見ていきます。
360
+
361
+
362
+
363
+ > [class.base.init/2](https://timsong-cpp.github.io/cppwp/n4861/class.base.init#2)
364
+
365
+ >
366
+
367
+ > In a __mem-initializer-id__ an initial unqualified __identifier__ is looked up in the scope of the constructor's class and, if not found in that scope, it is looked up in the scope containing the constructor's definition.
368
+
369
+ [ Note: If the constructor's class contains a member with the same name as a direct or virtual base class of the class, a __mem-initializer-id__ naming the member or base class and composed of a single identifier refers to the class member.
370
+
371
+ A __mem-initializer-id__ for the hidden base class may be specified using a qualified name.
372
+
373
+ — end note
374
+
375
+ ]
376
+
377
+ **Unless the __mem-initializer-id__ names the constructor's class, a non-static data member of the constructor's class, or a direct or virtual base of that class, the __mem-initializer__ is ill-formed.**
378
+
379
+
380
+
381
+ とくに太字で強調したUnless以降が大事なのでまとめてみます。これによれば、有効な`mem-initializer-id`とは次の3つです。
382
+
383
+
384
+
385
+ - コンストラクタを書いているクラス自身の名前: 移譲コンストラクタのこと
386
+
387
+ - コンストラクタを書いているクラス自身の`static`指定されていないメンバー変数の名前
388
+
389
+ - 基底クラス(仮想基底クラスを含む)の名前
390
+
391
+
392
+
393
+ これ以外を指定した場合、ill-formedである、と書かれています。
394
+
395
+
396
+
397
+ これはどういうことかと言うと、メンバー初期化リストの時点では基底クラスは初期化されていないためアクセスできないということだと考えられます。
398
+
399
+
400
+
401
+ 以上の内容は`base class protected member init direct`でググって一番上に出てきた
402
+
403
+ [inheritance - Initialize parent's protected members with initialization list (C++) - Stack Overflow](https://stackoverflow.com/questions/2290733/initialize-parents-protected-members-with-initialization-list-c)
404
+
405
+ を元に書かれています。
406
+
407
+
408
+
409
+ ### 結論
410
+
411
+
412
+
413
+ 基底クラスのメンバー変数は有効な`mem-initializer-id`ではないのでill-formed
414
+
415
+
416
+
417
+ ## protectedメンバーを使うことは妥当か
418
+
419
+
420
+
421
+ > しかしそれでは各Iteratorにカウンタ操作を指定するoperatorを記載せねばならず
422
+
423
+ コードのサイズが増えてしまうように感じるのですが
424
+
425
+ それでも依存関係の簡略化を優先したほうが良いのでしょうか。
426
+
427
+
428
+
429
+ C++のイテレータを作るのがとっても面倒なのはものすごくよくわかります。
430
+
431
+
432
+
433
+ 実際にSTLの実装を覗いてみることにします。
434
+
435
+
436
+
437
+ ### msvcの実装
438
+
439
+
440
+
441
+ [https://github.com/microsoft/STL/blob/master/stl/inc/vector](https://github.com/microsoft/STL/blob/master/stl/inc/vector)
442
+
443
+ [https://github.com/microsoft/STL/blob/master/stl/inc/xmemory](https://github.com/microsoft/STL/blob/master/stl/inc/xmemory)
444
+
445
+
446
+
447
+ これは`_Vector_iterator`→`_Vector_const_iterator`→`_Iterator_base`という継承になっていて、operator類はそれぞれ定義しているのがわかります。
448
+
449
+
450
+
451
+ ### libstdc++の実装
452
+
453
+
454
+
455
+ [https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/include/bits/stl_vector.h](https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/include/bits/stl_vector.h)
456
+
457
+ [https://github.com/gcc-mirror/gcc/blob/master/libstdc++-v3/include/bits/stl_iterator.h](https://github.com/gcc-mirror/gcc/blob/master/libstdc++-v3/include/bits/stl_iterator.h)
458
+
459
+
460
+
461
+ イテレータに関しては極めてシンプルで、iteratorとconst_iteratorは同じ`__normal_iterator`クラスでできています。protectedメンバーがありますが、std::vectorから使う文についてはprotectedである必要を見いだせないので多分別の用途で使ってるのでしょう。
462
+
463
+
464
+
465
+ ### 結論
466
+
467
+
468
+
469
+ 言い出しておいてなんですが正直わからんです。自分はC++のイテレータに関してはKISSの法則を投げ捨てたほうがうまくいく気がします。比較系は先述の通り三方比較演算子のおかげで自動定義がある程度使えますが、その他は愚直に書くのがなんだかんだいいのかなと思っています。依存名か非依存名かでうっかり引っかかったりする心配もないですし。
470
+
471
+
472
+
473
+ 本件とは別の問題点からになりますが、これまでイテレータを作るときに継承するべきとされていた`std::iterator`はC++17でdeprecatedになりました。まあそんなこんなであんまり継承したくない心情です。
474
+
475
+ [P0174R2 Deprecating Vestigial Library Parts in C++17](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0174r2.html)

1

m

2020/08/11 15:54

投稿

yumetodo
yumetodo

スコア5850

test CHANGED
@@ -255,3 +255,11 @@
255
255
  }
256
256
 
257
257
  ```
258
+
259
+
260
+
261
+ [https://wandbox.org/permlink/IJJvIQtlluedZIRj](https://wandbox.org/permlink/IJJvIQtlluedZIRj)
262
+
263
+
264
+
265
+ ちなみにC++20を使えるなら三方比較演算子を使うと楽になると思います。