Ошибка вызова функции без сопоставления при введении пространства именC++

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

Сообщение Anonymous »

Следующий код работает, как и ожидалось: < /p>

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

template  struct Int {
constexpr auto dec() const -> Int {
return {};
}
constexpr operator int() const {
return N;
}
};

constexpr auto fib(Int) -> Int {
return {};
}
template  constexpr auto fib(Int n) -> decltype(fib(n.dec())) {
return {};
}

static_assert(fib(Int()) == 0);
Однако, если я представляю пространство имен для int , оно внезапно не удастся как на GCC, так и в Clang:
namespace foo {
template struct Int {
constexpr auto dec() const -> Int {
return {};
}
constexpr operator int() const {
return N;
}
};
}

template using Int = foo::Int;

constexpr auto fib(Int) -> Int {
return {};
}
template constexpr auto fib(Int n) -> decltype(fib(n.dec())) {
return {};
}

static_assert(fib(Int()) == 0);
< /code>
Со следующим сообщением об ошибке: < /p>
:21:15: error: no matching function for call to 'fib'
21 | static_assert(fib(Int()) == 0);
| ^~~
:14:16: note: candidate function not viable: no known conversion from 'Int' to 'Int' for 1st argument
14 | constexpr auto fib(Int) -> Int {
| ^ ~~~~~~
:17:33: note: candidate template ignored: substitution failure [with N = 2]: call to function 'fib' that is neither visible in the template definition nor found by argument-dependent lookup
17 | template constexpr auto fib(Int n) -> decltype(fib(n.dec())) {
| ^ ~~~
< /code>
Может ли кто -нибудь объяснить, что здесь происходит? Почему представление пространства имен создает ошибку компилятора?

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

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

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

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

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

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

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