1

This is my (maybe a little bit weird) thought, suppose I want to define a std::set object to contain some stuff for later use, but unfortunately I'm not sure which type will be passed to std::set<not-sure> as template arg, and this not-sure type will be determined through a string, like this:

class X {
public:

foo()
{
    char not_sure_type[20]; 
    scanf("%s", not_sure_type);
    if (strcmp(not_sure_type, "int"))
        // then std::set<int>
    else if (// "char")
        // then std::set<char>
}

private:
void * _set;

};

This way, I can determine that std::set<int> will be instantiated or not, right? But how can I tell _set that you should point to a std::set<int>? Without knowing that, either I cannot use static_cast to cast _set from void * to std::set<int>*, back and forth.

So can I save the std::set<int> just like an data member for later use?

Any idea is appreciated.

4
  • What is the actual use case? This sounds like exactly what templates are for. You definitely can't access a type by name though, unless that class specifically registers itself in some kind of table. Jan 8, 2013 at 10:06
  • @DavidSchwartz, do you mean if I have std::set<some_type> registered in a table, I can use it to instantiate an instance? How?
    – Alcott
    Jan 8, 2013 at 10:08
  • 1
    You can have some "base" class for not_sure_type an decide its derived classes based on the string. Later on use virtual functions to accomplish common tasks.
    – iammilind
    Jan 8, 2013 at 10:12
  • @iammilind, well, actually there won't be a virtual function to do any common tasks, it's just a type containing some data.
    – Alcott
    Jan 8, 2013 at 10:16

2 Answers 2

5

If you will know the the type of the set element at run-time (based on a say string), you could maybe store a pointer to an abstract type into the set (set), and then use an Abstract Factory in the constructor of the class that holds the std::set to instantiate the actual Concrete Types for the elements during run-time based on the provided string. The problem is in using raw pointers here, since you will need to do the cleanup within the class that has std::set. Since you want to use std::set, make sure that your Concrete Type for the element is Comparable. Not sure if this is the right way to go though.. you said to throw in ideas...

4
  • Do you mean that , if I determined it is std::set<int>, then I should do _set = new std::set<int>();, but later when I access _set, I should know which type it should be casted to before I access it, right? If so, how can I tell what type std::set<some_type> is at run-time?
    – Alcott
    Jan 8, 2013 at 10:24
  • In this case I think you need to encapsulate your type, meaning that you should avoid the use of base types directly, like std::set<int>, or any concrete type. You need to define an abstract type, say TypeStoredInSet, and derive all the stored types from this abstract type. This gives you the hierarchy of the AbstractType and ConcreteTypes of the Abstract Factory Pattern (link above). Then you need to create the AbstractFactory class that will be used to create the stored concrete types. If you opt for this solution, read the description of the pattern, it will get much clearer..
    – tmaric
    Jan 8, 2013 at 10:27
  • well, I have a problem implementing your idea now. I define an abstract class ATypeStoredInSet, then declare a set like this std::set<ATypeStoredInSet *>, later if the read-in string is int, then how? int cannot inherit ATypeStoredInSet, can it?
    – Alcott
    Jan 9, 2013 at 0:21
  • @Alcott within the class where you haev std::set<ATypeStoredInSet*> you can define a member function with a name CreateElement(string), which will have the logic for the type creation inside. The problem with the built in types is as I said before: you need to define something like myInt that wraps the built in "int" type and inherits from ATypeStoredInSet in order to store it in the set, int is not ATypeStoredInSet* ... If you need to work with built in types, that's the way to go..
    – tmaric
    Jan 9, 2013 at 8:12
0

sounds to me like you are considering using c++ as a weak type language, such as python. sure there could be workarounds like using some abstract base class etc. but the bottom line I think is that defining the type at run time is against the paradigm of c++..

6
  • 1
    I strongly dissagree with this. I'm working with OpenFOAM, which is a C++ library for CFD used intensively all over the world, and it uses a mechanism called Runtime Selection which is basically determining types of various transport models, boundary conditions, turbulence models, etc at runtime using a string-based type-name system defined through dictionary entries in text files (strings!).
    – tmaric
    Jan 8, 2013 at 10:35
  • @tomislav-maric, babe that's what I'm talking about, I'm trying to determine which type to instantiate through some strings stored in a text file.
    – Alcott
    Jan 8, 2013 at 10:38
  • @Alcott Cool! Then you either need Abstract Factory or Factory Mettod for this. That's how this is solved in OpenFOAM: you have dictionary (read text file) entries defining the types that are then "selected" (read created by a Factory) during run-time.
    – tmaric
    Jan 8, 2013 at 10:40
  • for types that are related - share the same base class for example - this maybe makes sense but for 'int' and 'char'...
    – WeaselFox
    Jan 8, 2013 at 10:41
  • @WeaselFox: that's what I wrote in the answer.. if they relate to the same interface, you're fine. Even if you encapsulate something as differnt as char and int, as long as the resulting types share an interface, you can use the Factory approach.
    – tmaric
    Jan 8, 2013 at 10:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.