C++11 will have tons of new features and concepts. One among them is
N2765: user-defined literals (scheduled for GCC 4.7). Quite possible I'm dumb
and ugly, but frankly, "user-defined literals" is something I've so many doubts
about. There're already lots of examples, though I haven't seen anything really
neat so far (only speaking for myself).
For example:
constexpr long double operator"" _degrees (long double d)
{
return d * 0.0175;
}
long double pi = 180_degrees;
Or (link)
typedef std::map MyMap;
MyMap create_map()
{
MyMap m;
m["lol"] = 7;
return m;
}
auto m = create_map();
int& operator "" m(const char *key, size_t length)
{
return m[key];
}
int main(void)
{
std::cout << "lol"m << std::endl;
// 7
"lol"m = 2;
std::cout << "lol"m << std::endl;
// 2
return 0;
}
Or (link)
template struct __checkbits
{
static const bool valid = false;
};
template struct __checkbits
{
static const bool valid = (High == '0' || High == '1')
&& __checkbits::valid;
};
template struct __checkbits
{
static const bool valid = (High == '0' || High == '1');
};
template
inline constexpr std::bitset
operator"" _bits() noexcept
{
static_assert(__checkbits::valid, "invalid digit in binary string");
return std::bitset((char []){Bits..., '\0'});
}
int main()
{
auto bits = 010101010101010101010101010101010101010101_bits;
std::cout << bits << std::endl;
std::cout << "size = " << bits.size() << std::endl;
std::cout << "count = " << bits.count() << std::endl;
std::cout << "value = " << bits.to_ullong() << std::endl;
// This triggers the static_assert at compile time.
auto badbits = 21010101010101010101010101010101010101_bits;
// This throws at run time.
std::bitset<64> badbits2("21010101010101010101010110101010101_bits");
}
-ss
N2765: user-defined literals (scheduled for GCC 4.7). Quite possible I'm dumb
and ugly, but frankly, "user-defined literals" is something I've so many doubts
about. There're already lots of examples, though I haven't seen anything really
neat so far (only speaking for myself).
For example:
constexpr long double operator"" _degrees (long double d)
{
return d * 0.0175;
}
long double pi = 180_degrees;
Or (link)
typedef std::map
MyMap create_map()
{
MyMap m;
m["lol"] = 7;
return m;
}
auto m = create_map();
int& operator "" m(const char *key, size_t length)
{
return m[key];
}
int main(void)
{
std::cout << "lol"m << std::endl;
// 7
"lol"m = 2;
std::cout << "lol"m << std::endl;
// 2
return 0;
}
Or (link)
template
{
static const bool valid = false;
};
template
{
static const bool valid = (High == '0' || High == '1')
&& __checkbits
};
template
{
static const bool valid = (High == '0' || High == '1');
};
template
inline constexpr std::bitset
operator"" _bits() noexcept
{
static_assert(__checkbits
return std::bitset
}
int main()
{
auto bits = 010101010101010101010101010101010101010101_bits;
std::cout << bits << std::endl;
std::cout << "size = " << bits.size() << std::endl;
std::cout << "count = " << bits.count() << std::endl;
std::cout << "value = " << bits.to_ullong() << std::endl;
// This triggers the static_assert at compile time.
auto badbits = 21010101010101010101010101010101010101_bits;
// This throws at run time.
std::bitset<64> badbits2("21010101010101010101010110101010101_bits");
}
-ss
No comments:
Post a Comment