VS2010 Compiler define
Asked Answered
S

3

6

In gcc I am writting friend class FriendMaker<T>::Type but Visual Studio wants friend FriendMaker<T>::Type. So I think it is time to go compiler specific.

So What I need to ifdef for Visual Studio ? I am using 2010 at the moment but I may switch to 2012 latter.

Snuck answered 1/8, 2012 at 7:34 Comment(0)
G
17

Use the macro _MSC_VER. To check if the compiler is VS2010, or above:

#if _MSC_VER >= 1600

The following are values for the different versions of VS:

  • VS 2003 (VC7.1): 1310
  • VS 2005 (VC8): 1400
  • VS 2008 (VC9): 1500
  • VS 2010 (VC10): 1600
  • VS 2012 (VC11): 1700
Goodhen answered 1/8, 2012 at 7:37 Comment(5)
@NeelBasu, see the linked page. That is what Microsoft decided.Goodhen
So Can I Safely make it 1600 <= _MSC_VER such that It also supports the latter versions ?Snuck
But the last edit doesn't filter VS2010 and above that previous one was doing.Snuck
No issues I personally prefer _MSC_VER >= 1600 but as the answer author used the reverse one. I mentioned it that way in comment.Snuck
@NeelBasu, I had originally used == in my answer. Putting literals (always const) on the left of equality checks is just a habit from code.Goodhen
N
0

Just use the friend class ... syntax for both compilers. The friend ... syntax, without the class keyword, is actually invalid; VS2010 is incorrect in not complaining about it.

See this question.

Neumeyer answered 1/8, 2012 at 8:0 Comment(1)
Hmm. I just tried this with Visual C++ 2010 Express: class One { friend class Two; }; class Two { friend class One; }; int main() { One o1; Two o2; }, and it merely warned about the unused variables. What exactly is the error message you get?Neumeyer
N
0

I think you need to use following code for cross compiler:

template <typename T> class B;

template <typename T>
class A
{
    friend typename B<T>::V;
};
Nankeen answered 11/2, 2014 at 21:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.