大约有 118 项符合查询结果(耗时:0.0177秒) [XML]
Random float number generation
...hod.
This will generate a number from 0.0 to 1.0, inclusive.
float r = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
This will generate a number from 0.0 to some arbitrary float, X:
float r2 = static_cast <float> (rand()) / (static_cast <float> (RAND_M...
What happens if you static_cast invalid value to enum class?
...g type to the enumeration type, no value in data[0] can lead to UB for the static_cast.
After CWG 1766 (C++17)
See CWG defect 1766.
The [expr.static.cast]p10 paragraph has been strengthened, so you now can invoke UB if you cast a value that is outside the representable range of an enum to the enum ...
[精华] VC中BSTR、Char和CString类型的转换 - C/C++ - 清泛网 - 专注C/C++及内核技术
...
const wchar_t *
Right:
BSTR bs = ...; //
...
LPCTSTR sz = static_cast<LPCTSTR>bs;
...
::SysFreeString(bs);
//Never use sz after this line
Wrong:
BSTR bs = ...; //
...
LPCTSTR sz = bs;
...
::SysFreeString(bs);
//Never use sz after this line
_tcslen(sz); ...
What is the curiously recurring template pattern (CRTP)?
...p1, Equality<Derived> const & op2)
{
Derived const& d1 = static_cast<Derived const&>(op1);//you assume this works
//because you know that the dynamic type will actually be your template parameter.
//wonderful, isn't it?
Derived const& d2 = static_cast...
How can I iterate over an enum?
...Last
};
for ( int fooInt = One; fooInt != Last; fooInt++ )
{
Foo foo = static_cast<Foo>(fooInt);
// ...
}
Please note, the enum Last is meant to be skipped by the iteration. Utilizing this "fake" Last enum, you don't have to update your terminating condition in the for loop to the las...
How do I assign an alias to a function name in C++?
...ame = old_fn_name;
If this function has multiple overloads you should use static_cast:
const auto& new_fn_name = static_cast<OVERLOADED_FN_TYPE>(old_fn_name);
Example: there are two overloads of function std::stoi
int stoi (const string&, size_t*, int);
int stoi (const wstring&...
What are the main purposes of using std::forward and which problems it solves?
...to get the same kind of value-category that we got! The solution is this:
static_cast<T&&>(x);
What does this do? Consider we're inside the deduce function, and we've been passed an lvalue. This means T is a A&, and so the target type for the static cast is A& &&, or...
C++11 emplace_back on vector?
... typedef T value_type;
value_type* allocate(size_t n) { return static_cast<value_type*>(::operator new(sizeof(value_type) * n)); }
void deallocate(value_type* p, size_t n) { return ::operator delete(static_cast<void*>(p)); }
template<class U, class... Args&...
Lambda capture as const reference?
...
In c++14 using static_cast / const_cast:
[&best_string = static_cast<const std::string&>(best_string)](const string& s)
{
best_string = s; // fails
};
DEMO
In c++17 using std::as_const:
[&best_string = std::...
Why does dividing two int not yield the right value when assigned to double?
...
Since the question is tagged C++ I would prefer to see static_cast<> rather than a C cast.
– Martin York
Sep 27 '11 at 15:41
16
...