this is my code:
#pragma once
#include <array>
constexpr unsigned int deck_size = 52;
constexpr std::array<char, 4> suits{'H', 'D', 'C', 'S'};
constexpr std::array<char, 13> ranks{'2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A'};
typedef std::pair<char, char> card;
typedef std::array<card, deck_size> deck;
constexpr deck initialize_cards()
{
deck cards;
//deck cards{};
int card_index = 0;
for (auto const &suit : suits)
{
for (auto const &rank : ranks)
{
cards[card_index] = {rank, suit};
// cards[card_index] = card{rank, suit};
++card_index;
}
}
return cards;
}
constexpr deck cards = initialize_cards();
it gives me following error:
In file included from main.cpp:2: models.hpp:26:40: in ‘constexpr’
expansion of ‘initialize_cards()’ models.hpp:18:44: error: call to
non-‘constexpr’ function ‘std::pair<_T1, _T2>& std::pair<_T1,
_T2>::operator=(std::__conditional_tstd::__and_<std::is_move_assignable<_Tp,
std::is_move_assignable<_T2> >::value, std::pair<_T1, _T2>&&,
std::__nonesuch&&>) [with _T1 = char; _T2 = char;
std::__conditional_tstd::__and_<std::is_move_assignable<_Tp,
std::is_move_assignable<_T2> >::value, std::pair<_T1, _T2>&&,
std::__nonesuch&&> = std::pair<char, char>&&]’ 18 |
cards[card_index] = {rank, suit};
| ^ In file included from /usr/include/c++/13/bits/stl_algobase.h:64,
from /usr/include/c++/13/string:51,
from /usr/include/c++/13/bits/locale_classes.h:40,
from /usr/include/c++/13/bits/ios_base.h:41,
from /usr/include/c++/13/ios:44,
from /usr/include/c++/13/ostream:40,
from /usr/include/c++/13/iostream:41,
from main.cpp:1: /usr/include/c++/13/bits/stl_pair.h:737:7: note: ‘std::pair<_T1, _T2>&
std::pair<_T1,
_T2>::operator=(std::__conditional_tstd::__and_<std::is_move_assignable<_Tp,
std::is_move_assignable<_T2> >::value, std::pair<_T1, _T2>&&,
std::__nonesuch&&>) [with _T1 = char; _T2 = char;
std::__conditional_tstd::__and_<std::is_move_assignable<_Tp,
std::is_move_assignable<_T2> >::value, std::pair<_T1, _T2>&&,
std::__nonesuch&&> = std::pair<char, char>&&]’ declared here 737 |
operator=(__conditional_t<_and<is_move_assignable<_T1>,
| ^~~~~~~~ In file included from models.cpp:1: models.hpp:26:40: in ‘constexpr’ expansion of ‘initialize_cards()’
models.hpp:18:44: error: call to non-‘constexpr’ function
‘std::pair<_T1, _T2>& std::pair<_T1,
_T2>::operator=(std::__conditional_tstd::__and_<std::is_move_assignable<_Tp,
std::is_move_assignable<_T2> >::value, std::pair<_T1, _T2>&&,
std::__nonesuch&&>) [with _T1 = char; _T2 = char;
std::__conditional_tstd::__and_<std::is_move_assignable<_Tp,
std::is_move_assignable<_T2> >::value, std::pair<_T1, _T2>&&,
std::__nonesuch&&> = std::pair<char, char>&&]’ 18 |
cards[card_index] = {rank, suit};
| ^ In file included from /usr/include/c++/13/bits/stl_algobase.h:64,
from /usr/include/c++/13/array:43,
from models.hpp:2: /usr/include/c++/13/bits/stl_pair.h:737:7: note: ‘std::pair<_T1, _T2>&
std::pair<_T1,
_T2>::operator=(std::__conditional_tstd::__and_<std::is_move_assignable<_Tp,
std::is_move_assignable<_T2> >::value, std::pair<_T1, _T2>&&,
std::__nonesuch&&>) [with _T1 = char; _T2 = char;
std::__conditional_tstd::__and_<std::is_move_assignable<_Tp,
std::is_move_assignable<_T2> >::value, std::pair<_T1, _T2>&&,
std::__nonesuch&&> = std::pair<char, char>&&]’ declared here 737 |
operator=(__conditional_t<_and<is_move_assignable<_T1>,
| ^~~~~~~~