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.

Neel Basu
  • 12,638
  • 12
  • 82
  • 146

3 Answers3

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
hmjd
  • 120,187
  • 20
  • 207
  • 252
  • @NeelBasu, see the linked page. That is what Microsoft decided. – hmjd Aug 01 '12 at 07:39
  • So Can I Safely make it `1600 <= _MSC_VER` such that It also supports the latter versions ? – Neel Basu Aug 01 '12 at 07:40
  • But the last edit doesn't `filter VS2010 and above` that previous one was doing. – Neel Basu Aug 01 '12 at 07:44
  • No issues I personally prefer `_MSC_VER >= 1600` but as the answer author used the reverse one. I mentioned it that way in comment. – Neel Basu Aug 01 '12 at 07:56
  • @NeelBasu, I had originally used `==` in my answer. Putting literals (always `const`) on the left of equality checks is just a habit from code. – hmjd Aug 01 '12 at 07:58
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.

Community
  • 1
  • 1
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • 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? – Keith Thompson Aug 01 '12 at 08:09
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;
};