最初のプログラムがコンパイルされる理由を誰もが知っていますが、2番目のプログラムはコンパイルされませんか?唯一の違いは、最初のものは通常の機能を使用しますが、2番目のものはテンプレート機能を使用することです。テンプレート関数と非テンプレート関数のビットフィールドで過負荷解決の動作が異なるのはなぜですか?
回答する際は、標準の段落を参照してください。ありがとう。
a.cpp
struct X {
int x : 20;
int y : 12;
};
void f(const int& x) {}
void f(int&& x) {}
int main() {
X x;
f(x.x);
}
b.cpp
struct X {
int x : 20;
int y : 12;
};
template <typename T>
void f(T&& x) {}
template <typename T>
void f(const T& x) {}
int main() {
X x;
f(x.x);
}
コンパイラエラー:
[hidden]$ g++ -v 2>&1 | tail -n 1
gcc version 4.7.2 20120921 (Red Hat 4.7.2-2) (GCC)
[hidden]$ clang++ -v 2>&1 | head -n 1
clang version 3.3
[hidden]$ g++ -std=c++11 a.cpp
[hidden]$ g++ -std=c++11 b.cpp
b.cpp: In function ‘int main()’:
b.cpp:14:8: error: cannot bind bitfield ‘x.X::x’ to ‘int&’
[hidden]$ clang++ -std=c++11 a.cpp
[hidden]$ clang++ -std=c++11 b.cpp
b.cpp:14:5: error: non-const reference cannot bind to bit-field 'x'
f(x.x);
^~~
b.cpp:2:7: note: bit-field is declared here
int x : 20;
^
1 error generated.