
C++
- 16 ответов
- 0 вопросов
9
Вклад в тег
#include <iostream>
using namespace std;
using esp_spp_cb_event_t = int;
using esp_spp_cb_param_t = void;
void btCallback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) {
cout << "btCallback" << endl;
}
class MyBt {
public:
typedef void (*callback_type)(esp_spp_cb_event_t, esp_spp_cb_param_t *);
void register_callback(callback_type cb) {
cb(0, nullptr);
}
};
int main()
{
MyBt bt;
bt.register_callback(btCallback);
return 0;
}
p = p2
мы выполняем неявное преобразование встроенных типов, для которых в стандарте не описаны правила преобразования. То есть такое преобразование не гарантировано, но может быть реализовано компилятором, например#include <type_traits>
using namespace std;
int main() {
static_assert(std::is_convertible<int(*)[2], int(*)[]>::value, "Non-convertible");
return 0;
}
bot.register_next_step_handler(message, x1)
передается не функция x1, а значение типа int, которое записывается в global x1 после первого запуска функции x1.dumb_array & operator=( dumb_array temp)
, в котором temp инициализируется посредством метода dumb_array (dumb_array&& other)
, который перед выводом на экран сообщения operator=( dumb_array tmp) вызывает ещё один конструктор по умолчанию.#include <iostream>
using namespace std;
struct Foo {
Foo() { cout << "default ctor\n"; }
Foo(Foo&& arg) { cout << "move ctor\n"; }
};
void foo(Foo arg) {
cout << "foo\n";
}
int main () {
foo(Foo());
return 0;
}
In the initialization of an object, when the source object is a nameless temporary and is of the same class type (ignoring cv-qualification) as the target object.
Mandatory copy/move elision in Visual Studio
The C++ standard requires copy or move elision when the returned value is initialized as part of the return statement (such as when a function with return type Foo returns return Foo()). The Microsoft Visual C++ compiler always performs copy and move elision for return statements where it is required to do so, regardless of the flags passed to the compiler. This behavior is unchanged.
This is an optimization: even when it takes place and the copy/move (since C++11) constructor is not called, it still must be present and accessible (as if no optimization happened at all), otherwise the program is ill-formed