teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

微修正

2020/06/28 16:52

投稿

Chironian
Chironian

スコア23274

answer CHANGED
@@ -45,7 +45,7 @@
45
45
  【コメントを見て追記】
46
46
  試しに VC++ 2017で上記ソースをコンパイルしてみたら、おっしゃる通りのエラーがでました。
47
47
 
48
- 上記回答の前に実はラムダ式を使わないバージョンを作っていたのですが、そちらは通りました。
48
+ 上記回答の前に実はinlineとラムダ式を使わないバージョンを作っていたのですが、そちらは通りました。
49
49
 
50
50
  ```C++
51
51
  #include <iostream>

1

追記

2020/06/28 16:51

投稿

Chironian
Chironian

スコア23274

answer CHANGED
@@ -38,4 +38,65 @@
38
38
 
39
39
  }
40
40
  ```
41
- [wandbox](https://wandbox.org/permlink/ScKsyrW5YcGQeICV)
41
+ [wandbox](https://wandbox.org/permlink/ScKsyrW5YcGQeICV)
42
+
43
+
44
+ -----
45
+ 【コメントを見て追記】
46
+ 試しに VC++ 2017で上記ソースをコンパイルしてみたら、おっしゃる通りのエラーがでました。
47
+
48
+ 上記回答の前に実はラムダ式を使わないバージョンを作っていたのですが、そちらは通りました。
49
+
50
+ ```C++
51
+ #include <iostream>
52
+ #include <vector>
53
+
54
+ template<class Derived>
55
+ class Base {
56
+ private:
57
+ static const std::vector<const char*> paths;
58
+
59
+ static std::vector<const char*> init()
60
+ {
61
+ std::cout << "init()\n";
62
+ std::vector<const char*> paths;
63
+
64
+ // Derived::path がディレクトリ、path がその中のファイルとする。疑似コード。
65
+ paths.push_back(Derived::path);
66
+ paths.push_back(Derived::path);
67
+ paths.push_back(Derived::path);
68
+
69
+ return paths;
70
+ }
71
+ protected:
72
+ const std::vector<const char*> getPaths() const { return paths; }
73
+ };
74
+
75
+ template<class Derived>
76
+ const std::vector<const char*> Base<Derived>::paths=Base<Derived>::init();
77
+
78
+ class Derived : public Base<Derived> {
79
+ friend class Base<Derived>;
80
+ private:
81
+ static constexpr const char *path = "どこかのディレクトリへのパス";
82
+ public:
83
+ void print()
84
+ {
85
+ for (auto const* p : getPaths())
86
+ {
87
+ std::cout << p << "\n";
88
+ }
89
+ }
90
+ };
91
+
92
+ int main()
93
+ {
94
+ std::cout << "main()\n";
95
+ Derived derived;
96
+ derived.print();
97
+ }
98
+ ```
99
+ [wandbox](https://wandbox.org/permlink/J3SZWBOxc4bGotgh)
100
+
101
+ init()はmain()の前に走っていますが、たまたまかも知れません。
102
+ ただ、確かboostは、使っているように見せかけることでmain()より前に走らせていましたので、もしかするとある程度は当てにして良いかもしれません。