EDN Admin
Well-known member
Visual Studio 2012 Update 1 will fail to compile the following minimal program involvingpointer_traits<shared_ptr<void> >:#include <memory>
using namespace std;
int main(){
typedef pointer_traits<shared_ptr<void> >::element_type void_type;
return 0;
}
In the current implementation of pointer_traits, this will try to declare a functionstatic pointer pointer_to(element_type& _Val) where element_type is void, which is clearly not valid C++. The same problem exists for other smart pointer types likeunique_ptr or weak_ptr.
However, I would suppose that a standard-conforming implementation of pointer_traits should support the use ofpointer_traits<shared_ptr<void> > as long as no reference is made to thepointer_to method, which might result in undefined behaviour.
Furthermore, the Visual Studio pointer traits cannot be used forshared pointers to abstract types (except in some very special cases). For example, the following simple program will not compile:
#include <memory>
using namespace std;
class a{
virtual ~a(){}
virtual void abstract() = 0;
};
int main(){
typedef pointer_traits<shared_ptr<a> >::element_type a_type;
return 0;
}
While the first program can be fixed by providing an explicit specialization pointer_traits<shared_ptr<void> > in namespace std, Im not aware of a solution to the second problem other than replacing the VS2012pointer_traits by a self-written or third-party implementation. By the way: Both programs compile smoothly in GNU g++ 4.7.2.
So here are my questions about this issue:
View the full article
using namespace std;
int main(){
typedef pointer_traits<shared_ptr<void> >::element_type void_type;
return 0;
}
In the current implementation of pointer_traits, this will try to declare a functionstatic pointer pointer_to(element_type& _Val) where element_type is void, which is clearly not valid C++. The same problem exists for other smart pointer types likeunique_ptr or weak_ptr.
However, I would suppose that a standard-conforming implementation of pointer_traits should support the use ofpointer_traits<shared_ptr<void> > as long as no reference is made to thepointer_to method, which might result in undefined behaviour.
Furthermore, the Visual Studio pointer traits cannot be used forshared pointers to abstract types (except in some very special cases). For example, the following simple program will not compile:
#include <memory>
using namespace std;
class a{
virtual ~a(){}
virtual void abstract() = 0;
};
int main(){
typedef pointer_traits<shared_ptr<a> >::element_type a_type;
return 0;
}
While the first program can be fixed by providing an explicit specialization pointer_traits<shared_ptr<void> > in namespace std, Im not aware of a solution to the second problem other than replacing the VS2012pointer_traits by a self-written or third-party implementation. By the way: Both programs compile smoothly in GNU g++ 4.7.2.
So here are my questions about this issue:
- Am I right in assuming that the programs above are valid C++11?
- If so, does someone know about these or similar problems with pointer traits in VS2012?
- What might be the best way to get these pointer_traits working as expected?
View the full article