Непонятное разрешение шаблонаC++

Программы на C++. Форум разработчиков
Ответить Пред. темаСлед. тема
Anonymous
 Непонятное разрешение шаблона

Сообщение Anonymous »

Я выполнял упражнения из этого поста https://www.slamecka.cz/posts/2021-03-1 ... cisions-1/
Сначала а вообще хочу сказать огромное спасибо автору. Задачи были довольно интересными, сложными и увлекательными.
Одна часть задачи вызвала у меня некоторые проблемы, и я не совсем понимаю, почему.
< pre class="lang-cpp Prettyprint-override">

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

/**
* 18. Define Insert for Vector, it should take position, value and vector.
* Don't worry about bounds.
* Hint: use the enable_if trick, e.g.
*   template struct Foo;
*   template struct Foo {...};
*   template struct Foo {...};
*/

// Your code goes here:
// ^ Your code goes here

// static_assert(std::is_same_v);
// static_assert(std::is_same_v);
// static_assert(std::is_same_v);
// static_assert(std::is_same_v);
Я начал решать эту проблему, не обращая внимания на подсказку автора.
Это была моя первая попытка.

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

template  struct Insert;

template  struct Insert {
using type = Prepend::type;
};

template  struct Insert {
using type = typename Prepend::type;
};
В качестве сообщения об ошибке этого подхода компилятор жалуется на неоднозначность

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

E:\problems\main.cpp(212,20): error: ambiguous partial specializations of 'Insert'
[build]   212 |     std::is_same_v);
[build]       |                    ^
[build] E:\problems\main.cpp(163,49): note: partial specialization matches [with P = 0, I = 3, F = 4, N = ]
[build]   163 | template  struct Insert
> {
[build]       |                                                 ^
[build] E:\problems\main.cpp(167,35): note: partial specialization matches [with I = 3, N = ]
[build]   167 | template  struct Insert
Потом я прочитал подсказку и подумал, а что, если я просто использую std::conditional_t

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

template  struct Insert;

template  struct Insert {
using type = std::conditional_t<
P == 0, typename Prepend::type,
typename Prepend::type>;
};
И на этот раз я получил другое сообщение об ошибке

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

[build] E:\problems\main.cpp(178,36): error: implicit instantiation of undefined template '(anonymous namespace)::Insert'
[build]   178 |       typename Prepend::type>::type>;
[build]       |                                    ^
[build] E:\problems\main.cpp(178,36): note: in instantiation of template class '(anonymous namespace)::Insert' requested here
[build] E:\problems\main.cpp(178,36): note: in instantiation of template class '(anonymous namespace)::Insert' requested here
[build] E:\problems\main.cpp(222,20): note: in instantiation of template class '(anonymous namespace)::Insert' requested here
[build]   222 |     std::is_same_v);
[build]       |                    ^
[build] E:\problems\main.cpp(173,44): note: template is declared here
[build]   173 | template  struct Insert;
[build]       |                                            ^
Может ли кто-нибудь объяснить, почему я получаю эти сообщения и почему они различаются?


Подробнее здесь: https://stackoverflow.com/questions/790 ... resolution
Реклама
Ответить Пред. темаСлед. тема

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

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

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

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

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

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