I'd need a template which can be called like this:
int x = type_exists< std::vector<int> >::value;
This should set x to 1 if #include <vector>
was present (either explicitly or transitively) earlier in the source, otherwise it should set x to 0.
Is it possible to do it in C++? I'm using GCC, so GCC extensions are also fine.
It's also OK to change the call syntax a bit.
It's not OK to run the C++ compiler twice: first just to figure out if we get a compile error.
class_defined
would require addingCLASS_DEFINED_CHECK
to thevector
header, andhas_destructor
is a compile error for undefined classes. – Driveinvector
after your header? I'm really not seeing your use-case. – Interpretivevector
headers define thesupervector
template as well. My header includesvector
, and I'd like to use the system'ssupervector<int>
if available, otherwise, I'd like to use my fallback implementation. – Driveinhas_destructor
, you need to declare the name to test, then it works: coliru.stacked-crooked.com/a/4b39006dfae715d0 . A potential problem, as shown in the second part of themain
, is that if you declare a defined class you must do it at the same scope (and namespace) of the definition. – Wryneckhas_destructor
work for a template likevector<int>
? How do I pre-declare that? – Drivein__if_exists
. If not, well, sorry... – Lifelongvector
and why you don't want to check for the symbol_VECTOR_
? – Fritzschevector
is a problem. If you forward-declarenamespace std { template<class T, class Alloc> class vector; }
you can't use “std::vector<int>
” (missing 2nd template argument) if<vector>
wasn't included, but if you trynamespace std { template<class T> class allocator; template<class T, class Alloc = allocator<T> > class vector; }
then you will get an error (redefinition of default template argument) if<vector>
was included or later is. Sorry I can't find a solution at the moment. (Note that I linked the other question as “related”, not “duplicate”) – Wryneck<vector>
defines_VECTOR_
. With GCC 4.8.1 it doesn't; it defines_GLIBCXX_VECTOR
as an include guard (but I wouldn't rely on that). – Wrynecksection 17.4.3.1/3: "If the program declares or defines a name in a context where it is reserved, other than as explicitly allowed by this clause, the behavior is undefined."
So I guess that's the end of it. This link also says...I can't think of any way that this extension could break a conforming program, considering that users are not permitted to forward-declare standard library components...
– Moan