0

I am very new to modern C++ and am trying to do something as described below, which throws error on partial template specialization but works completely fine on complete specialization.

What am I doing here?

Language version: C++14 Compiler: GreenHills (GHS)

Doesnt work


template<bool isPrefix>
struct my_struct;


template<>
struct my_struct<true> // specializes true
{
   ... some data members...
   char prefix_data[10]

}

template<>
struct my_struct<false> // specializes false
{
   ... only other data members...
   // no prefix data member variable
}



template<bool isEnabled, class T>
void prefixData(const T& data)
{
    // performing some operation on T
    data.prefix_data[0] = 0x01;
}

// DOES NOT WORK, THROWS COMPILATION ERROR
template<class T>
void prefixData<false, T>(const T& data)
{
   // no logic required here
}

However, if I specify complete specialization for the function it works.

This Works

template<>
void prefixData<false, my_struct<false>>(const T& data)
{
}

P.S: I want to avoid run-time polymorphism (abstract class + inheritance) since the code will run on embedded platform with limited resources

I want to avoid explicit specialization of each cases. I have explained here with just isPrefix in my_struct. My actual code contains more template variables.

0 Answers0