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

回答編集履歴

3

refine

2020/07/25 06:38

投稿

yohhoy
yohhoy

スコア6191

answer CHANGED
@@ -22,10 +22,10 @@
22
22
  decltype(test1::funcC)>::value);
23
23
  ```
24
24
 
25
- - 関数`a`は`std::function<int(int)>`型へと暗黙変換可能です。
25
+ - 関数`a`は`std::function<int(int)>`型へと変換可能です。
26
- - 関数`a`は関数ポインタ`int(*)(int)`型へと暗黙変換可能です。
26
+ - 関数`a`は関数ポインタ`int(*)(int)`型へと変換可能です。
27
- - クロージャオブジェクト`b`は`std::function<int(int)>`型へと暗黙変換可能です。
27
+ - クロージャオブジェクト`b`は`std::function<int(int)>`型へと変換可能です。
28
- - ラムダ式`[](int x){return x;}`は**何もキャプチャしないため**、クロージャオブジェクト`b`は関数ポインタ`int(*)(int)`型へと暗黙変換可能です。
28
+ - ラムダ式`[](int x){return x;}`は**何もキャプチャしないため**、クロージャオブジェクト`b`は関数ポインタ`int(*)(int)`型へと変換可能です。
29
29
 
30
30
  ----
31
31
 

2

add link

2020/07/25 06:38

投稿

yohhoy
yohhoy

スコア6191

answer CHANGED
@@ -50,4 +50,6 @@
50
50
  cout << funcA(10, {b}) << '\n'; // {}によりstd::functionの推論ガイドを利用
51
51
  cout << funcB(10, +b) << '\n'; // +により関数ポインタ型へ強制変換
52
52
  cout << funcC(10, +b) << '\n'; // +により関数ポインタ型へ強制変換
53
- ```
53
+ ```
54
+
55
+ (参考記事:[ラムダ式と単項+演算子 - 地面を見下ろす少年の足蹴にされる私](https://onihusube.hatenablog.com/entry/2019/04/19/204552))

1

refine

2020/07/25 05:31

投稿

yohhoy
yohhoy

スコア6191

answer CHANGED
@@ -13,7 +13,7 @@
13
13
 
14
14
  - `test1::funcA`の第2引数は`std::function<int(int)>`型を要求します。
15
15
  - `test1::funcB`の第2引数は関数ポインタ`int(*)(int)`型を要求します。
16
- - `test1::funcC`の第2引数は関数`int(int)`型を要求するように見えますが、C++のルールにより「関数ポインタ`int(*)(int)`型」に読み替えられます。
16
+ - `test1::funcC`の第2引数は関数`int(int)`型を要求するように見えますが、[C++のルール](https://timsong-cpp.github.io/cppwp/n4659/dcl.decl#dcl.fct-5)により「関数ポインタ`int(*)(int)`型」に読み替えられます。
17
17
 
18
18
  つまり`test1::funcB`と`test1::funcC`は同一シグネチャを持つ関数です。
19
19
  ```c++
@@ -44,8 +44,8 @@
44
44
  [https://wandbox.org/permlink/3CqrQylmIetOvP79](https://wandbox.org/permlink/3CqrQylmIetOvP79)
45
45
  ```C++
46
46
  cout << funcA(10, {a}) << '\n'; // {}によりstd::functionの推論ガイドを利用
47
- cout << funcB(10, a) << '\n'; // (無加工)
47
+ cout << funcB(10, a) << '\n'; // (無加工)
48
- cout << funcC(10, a) << '\n'; // (無加工)
48
+ cout << funcC(10, a) << '\n'; // (無加工)
49
49
 
50
50
  cout << funcA(10, {b}) << '\n'; // {}によりstd::functionの推論ガイドを利用
51
51
  cout << funcB(10, +b) << '\n'; // +により関数ポインタ型へ強制変換