36
loading...
This website collects cookies to deliver better user experience
void delay(float milliseconds) { ... }
/*void delay_m(float minutes);
void delay_s(float seconds);
void delay_millis(float milliseconds);
void delay_micros(float microseconds);
void delay_ns(float nanoseconds);*/
// ... A class function ...
float initial_delay_ns = 1000;
delay_ns(initial_delay_ns);
// ...
float increased_delay_s = 1;
delay_s(increased_delay_s);
// ...
auto current_delay_ns = initial_delay_ns + increased_delay_s * 1e9;
auto wait_time_m = 2.65;
delay_m(wait_time_m);
// ...
std::cout << "Total delay: " << current_delay_ns + wait_time_m * 60 * 1e9 << std::endl;
// ...
Hi there.
I heard some words about a way to mark your numbers, so you'll be able to treat them in any way you want to. This mark can leave them as they are and can even transform them into a different type.
Note: This ability also relevant for chars and chars array.
It's called User-defined literals.
Sincerely yours,
cppreference.
// void delay(std::chrono::nanoseconds) {}
// ...
using namespace std::literals::chrono_literals;
auto initial_delay = 1000ns;
delay(initial_delay);
// ...
auto increased_delay = 1s;
delay(increased_delay);
// ...
auto current_delay = initial_delay + increased_delay;
auto wait_time = 3min;
delay(std::chrono::duration_cast<std::chrono::nanoseconds>(wait_time));
// ...
std::cout << "Total delay: " << std::chrono::duration_cast<std::chrono::seconds>(current_delay + wait_time).count() << " seconds." << std::endl;
// ...
out_type
operator"" _suffix
(in_type
);const char*
unsigned long long int
long double
char
wchar_t
char8_t
(sice C++20)char16_t
char32_t
const char*
, std::size_t
const wchar_t*
, std::size_t
const char8_t*
, std::size_t
const char16_t*
, std::size_t
const char32_t*
, std::size_t
template <char...> out_type operator"" _suffix();
struct A { constexpr A(const char *); };
template<A a> A operator"" _a();
using namespace std::;
. Without it the syntax would look like this:auto str = std::string_literals::operator""s("aaa", 3);
// Instead Of: auto str = "aaa"s;
using namespace ...
which usually considered as a bad practice, so why do we need it here?std::cout
).using namespace std::string_literals;
std::string_literals::operator""s
which accepts the following parameters:std::basic_string
object of the specified type. For example:auto str = "Hello String Literals"s;
std::basic_string
which is the same as std::string
. This one is a very common usage of user-defined literals.using namespace std::chrono_literals;
std::chrono::duration
object. It supplies the following user-defined literals: h
[hours], min
[minutes], s
[seconds], ms
[milliseconds], us
[microseconds], ns
[nanoseconds], y
[A specific year in the range: (-32767, 32767)], d
[Representing a day of a month in the calendar (legal only for values lower than 256)].using namespace std::literals::complex_literals
i
- returns std::complex(0, arg);
if
- returns std::complex(0, arg);
il
- returns std::complex(0, arg);
int main() {
using namespace std::complex_literals;
std::complex<double> c = 1.0 + 1i; // std::complex<double>(1.0, 1.0)
std::complex<float> z = 3.0f + 4.0if; // std::complex<float>(3.0, 4.0)
}
class angle {
public:
struct degrees {};
struct radians {};
constexpr angle(float deg, degrees) { _deg = deg; }
constexpr angle(float rad, radians) { _deg = rad * 180 / M_PI; }
[[nodiscard]] constexpr float get_degrees() const { return _deg; }
[[nodiscard]] constexpr float get_radians() const { return _deg * M_PI / 180; }
private:
float _deg;
};
namespace literals {
constexpr angle operator"" _deg(long double deg) {
return angle(deg, angle::degrees{});
}
constexpr angle operator"" _deg(unsigned long long int deg) {
return angle(deg, angle::degrees{});
}
constexpr angle operator"" _rad(long double rad) {
return angle(rad, angle::radians{});
}
constexpr angle operator"" _rad(unsigned long long int rad) {
return angle(rad, angle::radians{});
}
}
using namespace literals;
int main() {
constexpr auto deg = 3.14159265_rad;
constexpr auto rad = 360_deg;
static_assert(deg.get_degrees() == 180);
static_assert(rad.get_radians() == (float)(M_PI * 2));
std::cout << deg.get_degrees() << std::endl; // 180
std::cout << deg.get_radians() << std::endl; // 3.14..
std::cout << rad.get_degrees() << std::endl; // 360
std::cout << rad.get_radians() << std::endl; // 6.28..
return EXIT_SUCCESS;
}
struct date_offset {
int d;
int m;
int y;
constexpr date_offset(int days, int months, int years) : d(days), m(months), y(years) {}
[[nodiscard]] constexpr date_offset operator+(date_offset ref) const {
return date_offset(d + ref.d, m + ref.m, y + ref.y);
}
[[nodiscard]] constexpr date_offset operator-(date_offset ref) const {
return *this + date_offset(-ref.d, -ref.m, -ref.y);
}
};
class date {
public:
constexpr date(int day, int month, int year)
: d(day), m(month), y(year) {
self_balance();
}
[[nodiscard]] constexpr date offset(date_offset offset_data) const {
const auto after_years_offset = date(d, m, y + offset_data.y);
const auto after_month_offset = date(d, m + offset_data.m, after_years_offset.get_year());
return date(after_month_offset.get_day() + offset_data.d, after_month_offset.get_month(), after_month_offset.get_year());
}
[[nodiscard]] constexpr date operator+(date_offset offset_data) const {
return offset(offset_data);
}
[[nodiscard]] constexpr unsigned short get_day() const { return d; }
[[nodiscard]] constexpr unsigned short get_month() const { return m; }
[[nodiscard]] constexpr unsigned short get_year() const { return y; }
private:
int d, m, y;
constexpr void self_balance() {
unsigned short days_in_month;
unsigned short days_in_prev_month = 31;
bool is_change_detected = false;
if (m == 2) {
if (!(y % 4)) {
days_in_month = 29;
} else {
days_in_month = 28;
}
} else {
if (m <= 7 && m % 2 || m >= 8 && m % 2 == 0) {
days_in_month = 31;
if (m != 8) {
days_in_prev_month = 30;
}
} else {
days_in_month = 30;
}
}
if (d > days_in_month) {
d -= days_in_month;
m++;
is_change_detected = true;
} else if (d < 1) {
d += days_in_prev_month;
m--;
is_change_detected = true;
}
if (m > 12) {
m = 1;
y++;
is_change_detected = true;
} else if (m < 1) {
m = 12 - m;
y--;
is_change_detected = true;
}
if (is_change_detected) self_balance();
}
};
namespace literals {
inline namespace dates_literals {
constexpr date_offset operator"" _d(unsigned long long int days) {
return date_offset(days, 0, 0);
}
constexpr date_offset operator"" _m(unsigned long long int months) {
return date_offset(0, months, 0);
}
constexpr date_offset operator"" _y(unsigned long long int years) {
return date_offset(0, 0, years);
}
}
}
using namespace literals;
int main() {
constexpr date my_date(23, 8, 2020);
constexpr auto new_date = my_date + 8_d + 3_m + 5_y;
std::cout << new_date.get_day() << " / " << new_date.get_month() << " / " << new_date.get_year() << std::endl;
static_assert(new_date.get_day() == 1 && new_date.get_month() == 12 && new_date.get_year() == 2025);
return EXIT_SUCCESS;
}