Size: a a a

2020 April 13

d

dukomok in pro.cxx
dukomok
template <typename T> T foo(T* p)
{
T tmp = *p; // tmp will have the type to which p points
// . . .
return tmp;
}
а этот чтобы показать что функцию можно отмечать не только через int
источник

Е

Егор in pro.cxx
dukomok
этот пример, чтобы показать что class и typename ничем не отличаются
наверное ты хотел написать typename, и до 17 плюсов это не совсем правда
источник

Е

Егор in pro.cxx
dukomok
а этот чтобы показать что функцию можно отмечать не только через int
а вообще милости прошу к нашему @supapro
источник

d

dukomok in pro.cxx
эти примеры в разделе defining a template
источник

d

dukomok in pro.cxx
Егор
а вообще милости прошу к нашему @supapro
туда не заходит
источник

Е

Егор in pro.cxx
dukomok
туда не заходит
надо было на кнопку нажать, ты в бане теперь
источник

d

dukomok in pro.cxx
у липпмана ошибки есть?
источник

ПК

Побитый Кирпич in pro.cxx
dukomok
у липпмана ошибки есть?
Пока нет
источник

d

dukomok in pro.cxx
не компилится же
источник

AT

Andrew Titov in pro.cxx
Дай полный пример кода.
источник

d

dukomok in pro.cxx
они полные
источник

ПК

Побитый Кирпич in pro.cxx
dukomok
не компилится же
Врёшь
источник

AT

Andrew Titov in pro.cxx
dukomok
они полные
Скинь его сюда (в идеале — на godbolt или wandbox). Не у всех есть книга.
источник

ПК

Побитый Кирпич in pro.cxx
dukomok
не компилится же
Ошибка у тебя
источник

d

dukomok in pro.cxx
Template Type Parameters
Our compare function has one template type parameter. In general, we can use
a type parameter as a type specifier in the same way that we use a built-in or
class type specifier. In particular, a type parameter can be used to name the return
type or a function parameter type, and for variable declarations or casts inside the
function body:
// ok: same type used for the return type and parameter
template <typename T> T foo(T* p)
{
T tmp = *p; // tmp will have the type to which p points
// . . .
return tmp;
}
Each type parameter must be preceded by the keyword class or typename:
// error: must precede U with either typename or class
template <typename T, U> T calc(const T&, const U&);
These keywords have the same meaning and can be used interchangeably inside a
template parameter list. A template parameter list can use both keywords:
// ok: no distinction between typename and class in a template parameter list
template <typename T, class U> calc (const T&, const U&);
It may seem more intuitive to use the keyword typename rather than class
to designate a template type parameter. After all, we can use built-in (nonclass)
types as a template type argument. Moreover, typename more clearly indicates
that the name that follows is a type name. However, typename was added to C++
after templates were already in widespread use; some programmers continue to
use class exclusively.
источник

ПК

Побитый Кирпич in pro.cxx
dukomok
Template Type Parameters
Our compare function has one template type parameter. In general, we can use
a type parameter as a type specifier in the same way that we use a built-in or
class type specifier. In particular, a type parameter can be used to name the return
type or a function parameter type, and for variable declarations or casts inside the
function body:
// ok: same type used for the return type and parameter
template <typename T> T foo(T* p)
{
T tmp = *p; // tmp will have the type to which p points
// . . .
return tmp;
}
Each type parameter must be preceded by the keyword class or typename:
// error: must precede U with either typename or class
template <typename T, U> T calc(const T&, const U&);
These keywords have the same meaning and can be used interchangeably inside a
template parameter list. A template parameter list can use both keywords:
// ok: no distinction between typename and class in a template parameter list
template <typename T, class U> calc (const T&, const U&);
It may seem more intuitive to use the keyword typename rather than class
to designate a template type parameter. After all, we can use built-in (nonclass)
types as a template type argument. Moreover, typename more clearly indicates
that the name that follows is a type name. However, typename was added to C++
after templates were already in widespread use; some programmers continue to
use class exclusively.
Где ошибка?
источник

d

dukomok in pro.cxx
тут не указан перед calc int это или что
источник

d

dukomok in pro.cxx
template <typename T, class U> calc (const T&, const U&);
источник

ПК

Побитый Кирпич in pro.cxx
dukomok
template <typename T, class U> calc (const T&, const U&);
template <typename T, typename U> T calc(const T&, const U&);
источник

d

dukomok in pro.cxx
но это же ошибка
источник