我对我写的这个模板很困惑,它应该“自动”推断出我传入的指针类型,但编译器似乎并不认为这两种类型是相同的,而我认为是,并且typeid()
在我这边。
#include <iostream>
template <auto* static_ptr_to_object>
struct Handle
{
// static_ptr_to_object should be a pointer to a Whale<char>
// Both of the following typedefs should result in types of Whale<char>
// When I cout typeid() of these types it prints out the same types
// However std::is_same_v returns false
using pointee_type1 = std::remove_pointer_t<decltype(static_ptr_to_object)>;
using pointee_type2 = decltype(*static_ptr_to_object); // The two are the same types
void showTypes()
{
//static_assert(std::is_same_v<pointee_type1, pointee_type2>);
// Uncommenting this line will make the static assert fail
std::cout << "Type of 'pointee_type1' = " << typeid(pointee_type1).name() << "\n";
std::cout << "Type of 'pointee_type2' = " << typeid(pointee_type2).name() << "\n";
if (typeid(pointee_type1) == typeid(pointee_type2))
std::cout << "Types are the same\n";
else std::cout << "Types are not the same\n";
}
bool b1 = std::is_integral_v<decltype(pointee_type1::member)>;
// Uncommenting the below line will make compilation fail
//bool b2 = std::is_integral_v<decltype(pointee_type2::member)>;
// pointee_type2 must be a class or namespace when followed by ::
// pointee_type2 left of '::' must be a class or namespace name
};
template <typename T>
struct Whale
{
T member;
};
Whale<char> whale;
int main()
{
Handle<&whale> handleToWhale;
handleToWhale.showTypes();
}
所以我认为这两种类型是相同的,并且 typeid()
运算符说它们是相同的,但是 std::is_same
返回 false
,并且根据编译器错误消息,它无法识别 pointee_type2
是类类型,因为它无法识别::运算符。
编辑:我对解引用指针的理解是错误的,this question relates to在某些情况下,指针取消引用将如何返回引用类型。
相关文章:
python - 断言错误 : Wrong values for d ['w' ] | deeple
c# - 为什么 TextInfo.ToTitleCase 在字母全部为大写的字符串上不能正常工作?
python - 如何过滤掉列表列中包含特定子序列的 Pandas DataFrame 中的行?
c++ - C++20 范围是否具有过滤器或 any_of 的值(非谓词)版本?
nuget - Cake NuGetRestore 找不到 nuget.exe
javascript - 为什么减少数组的长度会使最后一个元素未定义但长度不变?