The following does not compile
#include <iostream>
#include <type_traits>
// forward declaration of a type template
template<class T, class Alloc = std::allocator<T>> class std::vector;
template<class T>
struct is_vector : std::false_type { };
// using the forward declared type template
template<class T, class Alloc>
struct is_vector<std::vector<T, Alloc>> : std::true_type { };
#include <vector>
int main()
{
std::cout << is_vector<std::vector<int>>::value << std::endl;
}
I want to make sure that forward declared type templates (above the vector
) are actually non useable in specialization contexts and that it's not a faulty implementation to blame. Also why is this happening? I don't want to create an object of type vector<>
only use it as a tag to dispatch between specializations; doesn't inclusion of <vector>
at the point of instantiation suffice (call site of is_vector<>
) ?
Forward-declaring of a standard library container is undefined behavior (if you manage to compile it), so for std::vector
you have to #include <vector>
before defining is_vector
.
For your own types you can do it though:
// forward declaration of a type template
namespace my {
template<class T, class Alloc> class vector;
}
template<class T>
struct is_vector : std::false_type { };
// using the forward declared type template
template<class T, class Alloc>
struct is_vector<my::vector<T, Alloc>> : std::true_type { };
namespace my {
template<class T, class Alloc = std::allocator<T>> class vector {};
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.