我尝试将指针深度显示为整数个星号 - 例如int***
将为3
。但是我没有从类型中计算它,而是编写了代码,要求必须正确初始化每个级别的指针。这是我试过的:
#include <iostream>
#include <utility>
// no pointers involved
template <class T>
std::size_t get_pointer_level(T)
{
return 0;
}
// final value reached, returning depth
template <class T>
std::size_t get_pointer_level(std::pair<std::size_t, T> arg)
{
return arg.first;
}
// function that accummulates depth
template <class T>
auto get_pointer_level(std::pair<std::size_t, T*> arg)
{
return get_pointer_level(std::make_pair(arg.first+1, *arg.second));
}
// initial function called for pointer argument
template <class T>
auto get_pointer_level(T* arg)
{
return get_pointer_level(std::make_pair(std::size_t(1), *arg));
}
int main(void)
{
int a = 10;
auto b = &a; //int*
auto c = &b; //int**
auto d = &c; //int***
auto e = &d; //int****
std::cout << get_pointer_level(e) << std::endl; //4
}
我很确定只有单独使用它才能使它工作。我想像语法会是这样的:
get_pointer_level<int****>::value
有什么想法吗?
修改 谢谢你的解决方案!以下是我想要完成的最终功能:
template <std::size_t A, std::size_t B>
struct is_smaller
{
enum {value = (A < B)? 1 : 0};
};
template<std::size_t Target, typename T, std::size_t Actual = get_pointer_level<T>::value>
T value_at_level(T pointer)
{
static_assert(Actual==Target, "Invalid target level!");
return pointer;
}
template<std::size_t Target, typename T, std::size_t Actual = get_pointer_level<T*>::value,
typename = std::enable_if<is_smaller<Target, Actual>::value>::type>
auto value_at_level(T* pointer)
{
return value_at_level<Target>(*pointer);
}
int main()
{
int a = 5;
auto b = &a; //int*
auto c = &b; //int**
auto d = &c; //int***
auto e = &d; //int****
std::cout << "int from int****: " << value_at_level<0>(e) << std::endl; //ok
std::cout << "int* from int***: " << value_at_level<1>(d) << std::endl; //ok
std::cout << "int** from int**: " << value_at_level<2>(c) << std::endl; //ok
std::cout << "int*** from int*: " << value_at_level<3>(b) << std::endl; //error
std::cout << "int**** from int: " << value_at_level<5>(a) << std::endl; //error
}
答案 0 :(得分:9)
部分专业化练习:
#include <cstddef>
template<typename T, std::size_t S>
struct get_pointer_level_impl
{
static const std::size_t value = S;
};
template<typename T, std::size_t S>
struct get_pointer_level_impl<T*, S> : get_pointer_level_impl<T, S+1>
{
};
template<typename T>
struct get_pointer_level : get_pointer_level_impl<T, 0>
{
};
#include <iostream>
int main()
{
std::cout << get_pointer_level<int>::value << "\n";
std::cout << get_pointer_level<int*>::value << "\n";
std::cout << get_pointer_level<int**>::value << "\n";
std::cout << get_pointer_level<int***>::value << "\n";
}
输出:
0
1
2
3
答案 1 :(得分:8)
如果你可以将其称为get_pointer_level<int*>()
而不是get_pointer_level<int*>::value
,那么递归constexpr
功能就可以了:
#include <iostream>
#include <type_traits>
template <typename T>
constexpr int get_pointer_level()
{
return !std::is_pointer<T>::value ? 0 : 1 + get_pointer_level<typename std::remove_pointer<T>::type>();
}
int main() {
std::cout << get_pointer_level<int>() << '\n';
std::cout << get_pointer_level<int*>() << '\n';
std::cout << get_pointer_level<int**>() << '\n';
std::cout << get_pointer_level<int***>() << '\n';
std::cout << get_pointer_level<int****>() << '\n';
}
答案 2 :(得分:4)
使用辅助类来确定指针级别。
#include <iostream>
template <class T> struct PointerLevel
{
static const std::size_t value = 0;
};
template <class T> struct PointerLevel<T*>
{
static const std::size_t value = PointerLevel<T>::value + 1;
};
// no pointers involved
template <class T>
std::size_t get_pointer_level(T)
{
return PointerLevel<T>::value;
}
int main(void)
{
int a = 10;
auto b = &a; //int*
auto c = &b; //int**
auto d = &c; //int***
auto e = &d; //int****
std::cout << get_pointer_level(a) << std::endl;
std::cout << get_pointer_level(b) << std::endl;
std::cout << get_pointer_level(c) << std::endl;
std::cout << get_pointer_level(d) << std::endl;
std::cout << get_pointer_level(e) << std::endl; //4
}
输出:
0
1
2
3
4
答案 3 :(得分:1)
您如何看待使用typeid(您的类型).name()??
std::string s( typeid(int***).name());
size_t n = std::count(s.begin(), s.end(), "*");