Skip to content

typename を文脈に合うように修正 #161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

pojiro
Copy link

@pojiro pojiro commented Aug 15, 2022

以下の文脈では後段のContainerArrayであるべきと考え、修正をしました。

cpp-intro/024-more-array.md

Lines 171 to 203 in 276be36

`std::array<T,N>`を受け取って要素をすべて出力する関数を書いてみよう。
~~~cpp
template < typename Array >
void print( Array & c )
{
for ( std::size_t i = 0 ; i != c.size() ; ++i )
{
std::cout << c[i] ;
}
}
int main()
{
std::array<int, 5> a = {1,2,3,4,5} ;
print( a ) ;
}
~~~
関数`print`がテンプレートなのは任意の`T`と`N`を使った`std::array<T,N>`を受け取れるようにするためだ。
関数のリファレンスを引数として渡すと、関数の中で変更できてしまう。しかし、上の例のような関数`print`では、引数を書き換える必要はない。この関数を使う人間も、引数を勝手に書き換えないことを期待している。この場合、`const`を付けることで値の変更を防ぐことができる。
~~~cpp
template < typename Container >
void print( Container const & c )
{
for ( std::size_t i = 0 ; i != c.size() ; ++i )
{
std::cout << c[i] ;
}
}
~~~

@pojiro pojiro marked this pull request as ready for review August 15, 2022 11:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant