I have a bunch of template classes, based on an enumeration type. Here is the source code:
#include <iostream>
// An enum type
enum class ENUMR : unsigned char {
SYSTEM1,
SYSTEM2,
UNKNOWN
};
// template class; will later be specialized based on ENUMR enumerators
template<ENUMR S>
class System
{};
// specialized System class, for enumerator SYSTEM1
template<>
class System<ENUMR::SYSTEM1>
{
private:
static constexpr char identifier { 'G' };
};
// An observation class recorded from a certain System instance
template<ENUMR Sys,short int Freq>
class Obs {
public:
Obs():
m_system {System<Sys> {} }, // does not work
m_frequency {Freq}
{};
//{
// m_system = System<Sys> {}; // works
//}
private:
System<Sys> m_system;
short int m_frequency;
};
// dummy code to test the template classes
int main ()
{
System<ENUMR::SYSTEM1> s1;
System<ENUMR::UNKNOWN> s2;
System<ENUMR::SYSTEM1> s3 (System<ENUMR::SYSTEM1>);
Obs<ENUMR::SYSTEM1, 1> obs;
std::cout <<"\n";
return 0;
}
Initialization of the member (template) variable Obs::m_system
produces a compile-time error, when performed as m_system {System<Sys> {} }
. However, when the same variable is initialized as m_system = System<Sys> {};
the source codes gets compiled (the respective lines in the source code above are commented). Does that mean that the assignment operator works, but the copy constructor fails? If so, why? When compiled against the gcc version 4.8.3
i get the following error message:
test.cpp: In instantiation of ‘Obs<Sys, Freq>::Obs() [with ENUMR Sys = (ENUMR)0u; short int Freq = 1]’:
test.cpp:43:28: required from here
test.cpp:25:22: error: too many initializers for ‘System<(ENUMR)0u>’
m_frequency {Freq} {};
Aucun commentaire:
Enregistrer un commentaire