Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Partial specialization with MS C++ compiler

Status
Not open for further replies.

globos

Programmer
Nov 8, 2000
260
FR
I want to know if a type is a pointer during execution.
I know the mechanism to use is partial specialization, I have found this source code :
Code:
#include <assert.h>
#include <iostream>
using namespace std;

template<class G>
struct is_pointer_type
{
  static const bool value = false;
};

template<class G>
struct is_pointer_type<G*>
{
  static const bool value = true;
};


int main ()
{
  assert (!is_pointer_type<int>::value);
  assert (is_pointer_type<int*>::value);
  assert (is_pointer_type<int**>::value);

  return 0;
}
It compiles and works successfully with GNU g++.
But MS' C++ compiler can't compile this code (I have Visual Studio C++ 6).

I also know the boost library has already the facility I want. I have looked at the source code but I was not able to understand how it is handled with VC6.

Does someone know the trick?

--
Globos
 
> Does someone know the trick?
Upgrade your compiler.

Much of templating in VC6 is broken compared to what the latest compilers can do (and what is in the standard).

Your example also compiles fine using VC7.NET as well.
Code:
> cl /EHsc foo.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 13.10.3077 for 80x86
Copyright (C) Microsoft Corporation 1984-2002. All rights reserved.

foo.cpp
Microsoft (R) Incremental Linker Version 7.10.3077
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:foo.exe
foo.obj

And the old compiler for reference.
Code:
> cl /EHsc foo.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8804 for 80x86
Copyright (C) Microsoft Corp 1984-1998. All rights reserved.

foo.cpp
foo.cpp(8) : error C2252: 'value' : pure specifier can only be specified for functions
        foo.cpp(9) : see reference to class template instantiation 'is_pointer_type<G>' being compiled
foo.cpp(15) : error C2989: 'is_pointer_type<class ?? ::G *>' : template class has already been defined as a non-template
 class
foo.cpp(15) : error C2988: unrecognizable template declaration/definition
foo.cpp(8) : error C2252: 'value' : pure specifier can only be specified for functions
        foo.cpp(20) : see reference to class template instantiation 'is_pointer_type<int>' being compiled
foo.cpp(20) : error C2039: 'value' : is not a member of 'is_pointer_type<int>'
foo.cpp(20) : error C2065: 'value' : undeclared identifier
foo.cpp(8) : error C2252: 'value' : pure specifier can only be specified for functions
        foo.cpp(21) : see reference to class template instantiation 'is_pointer_type<int *>' being compiled
foo.cpp(21) : error C2039: 'value' : is not a member of 'is_pointer_type<int *>'
foo.cpp(8) : error C2252: 'value' : pure specifier can only be specified for functions
        foo.cpp(22) : see reference to class template instantiation 'is_pointer_type<int * *>' being compiled
foo.cpp(22) : error C2039: 'value' : is not a member of 'is_pointer_type<int * *>'

--
 
>Upgrade your compiler.

I would like too. Moreover, command line tools are free. But it's impossible to debug with VC++ 6 programs compiled with the last version of the command line tools.

So the problem remains.

--
Globos
 
Have you applied the latest service pack for VC6? It has some STL updates but I don't know whether it will fix your problem or not.
 
The latest VC6 service pack (v6) does not cope with partial specialization support.
I have found a trick that seems to work very well. Here is the article explaining the hint(comp.lang.c++.moderated) :

Here is a sample test, that I wrote based on the contents of the article above :
Code:
struct PointerShim
{
  PointerShim(const volatile void*); // no implementation
};

// These are the discriminating functions
char IsPtr(PointerShim); // no implementation is required
int IsPtr(...);          // no implementation is required

template<class T>
struct IsPointer
{
  enum { Value = (sizeof(IsPtr(*(T*)0)) == 1) };
};

#define is_pointer(Type)	IsPointer<Type>::Value


//Tests.

class A
{
};

#include <iostream>
using namespace std;

int main ()
{
  cout << "is_pointer(int) : " << is_pointer (int) << endl
       << "is_pointer(int*) : " << is_pointer (int*) << endl
       << "is_pointer(int**) : " << is_pointer (int**) << endl
       << "is_pointer(A) : " << is_pointer (A) << endl
       << "is_pointer(A*) : " << is_pointer (A*) << endl;

  return 0;
}

--
Globos
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top