0

I am tryng to implement a class selector, the function of this class is to select a specific trait of a class at compilation time. But I getting buggy Another requirement is that no static function has to be implement. My experience in metaprogramming is to low.

#include <stdio.h>
#include <iostream>


template<class OptionSelected>
struct ContainnerValue
{
    typedef OptionSelected selectedValue;
    typename selectedValue value;
};

////Enum definition
enum class EOptions
{
    Option_One,
    Option_two
};
////trait base definition
template<EOptions>
struct SelectorClass{};

template<>
struct SelectorClass<EOptions::Option_One>
{
    void printClass()
    {
        std::cout<<"Class Selection One"<<std::endl;
    }
};
typedef SelectorClass<EOptions::Option_One> Option_One;

template<>
struct SelectorClass<EOptions::Option_two>
{
   void printClass()
    {
        std::cout<<"Class Selection two"<<std::endl;
    }
};

typedef struct SelectorClass<EOptions::Option_two> Option_Two;

int main(void)
{
    ContainnerValue<Option_One>::value.printClass();
    printf("Hello World!\n");
    return 0;
}
Robert
  • 10,403
  • 14
  • 67
  • 117
Ricardo_arg
  • 520
  • 11
  • 28
  • `typename selectedValue` <- the `typename` keyword is only required when you're referring to a nested type (something with a `::`). See http://stackoverflow.com/questions/610245/where-and-why-do-i-have-to-put-the-template-and-typename-keywords – dyp Apr 11 '14 at 15:11
  • `selectedValue value;` is not a static data member; yet, you're trying to refer to it as if it were a static data member here: `ContainnerValue::value.printClass();` – dyp Apr 11 '14 at 15:12

1 Answers1

0

I feel this is a quite complex way to do what you want to do, but anyway, here's the minimal set of changes to make this work: change the following lines

typename selectedValue value;

void printClass()

ContainnerValue<Option_One>::value.printClass();

to

typedef selectedValue value;

static void printClass()

ContainnerValue<Option_One>::value::printClass();

That is, make printClass a static member function, and call it using the name of the class followed by ::, without an object.

iavr
  • 7,547
  • 1
  • 18
  • 53