Вернуть std :: Необязательно , где конструктор t является частнымC++

Программы на C++. Форум разработчиков
Ответить
Anonymous
 Вернуть std :: Необязательно , где конструктор t является частным

Сообщение Anonymous »

Я до сих пор не понимаю поведения std :: Необязательно в следующем коде:

Код: Выделить всё

class A
{
public:
// A(int x, int y) : x(x), y(y) {} // always compiles
private:
A(int x, int y) : x(x), y(y) {}    // does not compile if using o.emplace(x, y)
friend std::optional makeA(int x, int y);

int x, y;
};

std::optional makeA(int x, int y)
{
std::optional o;
if (x != 0 && y != 0) {
return A(x, y);
// o.emplace(x, y);
}
return o;
}
, если я сделаю конструктор как публичный, то в Makea (...) я могу использовать либо вернуть a (x, y) или o.emplace (x, y) , оба компилируются. ) не будет компилировать. Насколько я понимаю, прочитав ошибку сообщения о шаблонах: < /p>

Код: Выделить всё

error: no matching function for call to ‘std::optional::emplace(int&, int&)’
70 |     o.emplace(x, y);
|     ~~~~~~~~~^~~~~~
In file included from test.cpp:2:
/usr/include/c++/11/optional:871:9: note: candidate: ‘template std::enable_if_t std::optional::emplace(_Args&& ...) [with _Args = {_Args ...}; _Tp = A]’
871 |         emplace(_Args&&... __args)
|         ^~~~~~~
/usr/include/c++/11/optional:871:9: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/11/bits/move.h:57,
from /usr/include/c++/11/bits/stl_pair.h:59,
from /usr/include/c++/11/bits/stl_algobase.h:64,
from /usr/include/c++/11/memory:63,
from test.cpp:1:
/usr/include/c++/11/type_traits: In substitution of ‘template using enable_if_t = typename std::enable_if::type [with bool _Cond = false; _Tp = A&]’:
/usr/include/c++/11/optional:871:2:   required by substitution of ‘template std::enable_if_t std::optional::emplace(_Args&& ...) [with _Args = {int&, int&}]’
test.cpp:70:14:   required from here
/usr/include/c++/11/type_traits:2579:11: error: no type named ‘type’ in ‘struct std::enable_if’
2579 |     using enable_if_t = typename enable_if::type;
|           ^~~~~~~~~~~
In file included from test.cpp:2:
/usr/include/c++/11/optional:883:9: note: candidate: ‘template std::enable_if_t std::optional::emplace(std::initializer_list, _Args&& ...) [with _Up = _Up; _Args = {_Args ...}; _Tp = A]’
883 |         emplace(initializer_list __il, _Args&&... __args)
|         ^~~~~~~
/usr/include/c++/11/optional:883:9: note:   template argument deduction/substitution failed:
test.cpp:70:14: note:   mismatched types ‘std::initializer_list’ and ‘int’
70 |     o.emplace(x, y);
Класс A не подходит для конструкции. Но как это возможно?

Подробнее здесь: https://stackoverflow.com/questions/794 ... is-private
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C++»