1

I was looking for a way to get some kind of reflection on my C++ structs (to help with serializing as well as just data management in general). I found this answer: C++ preprocessor: avoid code repetition of member variable list

It seemed like a great solution, until I implemented it and Visual Studio's IntelliSense exploded. Red squiggles everywhere! It doesn't understand member variable names in a struct using the REFLECTABLE macro, even though the compiler does.

Did I implement it wrong? Is there something I can do to make IntelliSense understand the variable names? Is there a better alternative method of getting C++ reflection that plays nicely with IntelliSense? It has to be able to iterate over a struct's members as well as get the name of the variable at runtime.

Community
  • 1
  • 1
Jonathan
  • 752
  • 1
  • 9
  • 19

1 Answers1

1

You have three options you can do.

First, you can use the __INTELLISENSE__ to define an alternative for intellisense:

struct Person
{
    Person(const char *name, int age)
        :
        name(name),
        age(age)
    {
    }
private:
#ifdef __INTELLISENSE__
    const char * name;
    int age; 
#else
    REFLECTABLE
    (
        (const char *) name,
        (int) age
    )
#endif
};

However, if you don't want to repeat the member variables but want to get rid of the red lines, you can disable intellisense(explained here), since it appears to be completely broken.

Now, if you still want to have good diagnostics and code completion then you are better off using another IDE or editor. Since most IDE's or editors don't explode on this. For IDE's you can try KDevelop, CodeLite, Netbeans, Eclipse, QtCreator, etc... For editors you could try emacs, vim, sublime, etc...(ideally, with clang integration). They all provide better diagnostic and code completion than visual studio.

Paul Fultz II
  • 17,682
  • 13
  • 62
  • 59
  • Telling someone to switch IDEs is not helpful. Visual studio with VAX has a lot of awesome features which things like eclipse and NetBeans don't have. – Nicolas Holthaus Sep 23 '14 at 23:37
  • Thanks Paul, it's nice to get an answer from the source. I found another reply you made on the subject here: http://stackoverflow.com/a/20296699/1917447 I'm using that Boost Fusion solution for now. Are there any advantages your custom macro has over Fusion besides the extra work of declaring the variables in two places with Fusion? – Jonathan Sep 24 '14 at 00:43
  • 1
    @Jonathan The only real advantage of a custom macro is that you could extended it to support other things such as methods or attributes. However, if you only need to reflect on simple fields, there is a lot of advantages to using Boost.Fusion, such as all the algorithms that go with it(not just `for_each`), and it integrates well with other libraries like Boost.Geometry or Boost.Spirit. – Paul Fultz II Sep 24 '14 at 04:11