is_const
式を関数に変換することは可能ですか、test
それともトップ レベルの cv-qualifiers がテンプレート型推論中に無視されるため、これは不可能ですか?
int main()
{
using std::is_const;
const int x = 0;
int y = 0;
// move to "bool test()"
std::cout
<< "main, x: " << is_const<decltype(x)>::value << "\n" // true
<< "main, y: " << is_const<decltype(y)>::value << "\n" // false
;
std::cout
<< "test, x: " << test(x) << "\n" // false, I wanted true
<< "test, y: " << test(y) << "\n" // false
;
}
次のようなさまざまなバージョンを試してみましたが、失敗しました。
template<typename T>
bool test(T x)
{
return is_const<???>::value;
}
test
私は何かが欠けていないこと、そしてそのような関数を書くことは本当に不可能であることを確認したい. (可能であれば、C++03 バージョンが可能かどうかも知りたいです。)
ご検討をお願いいたします
アップデート
Mankarse のおかげで、右辺値参照の場合は型推定が異なることを知りました。
template<typename T> void t1(T x);
template<typename T> void t2(T& x);
template<typename T> void t3(T&& x);
const int x = 42;
int y = 0;
t1(x); // T = int: t1<int>(int x)
t1(y); // T = int: t1<int>(int x)
t2(x); // T = const int: t2<const int>(const int& x)
t2(y); // T = int: t2<int>(int& x)
t3(x); // T = const int&: t3<const int&>(const int& && x)
t3(y); // T = int&: t3<int&>(int& && x)