gpt4 book ai didi

c++ - 为什么这段代码会使 VC++ 编译器崩溃?

转载 作者:可可西里 更新时间:2023-11-01 16:21:59 24 4
gpt4 key购买 nike

我正在使用以下编译器:

Microsoft Visual C++ 2010

以下代码在编译时使编译器崩溃:

template<class T_> 
void crasher(T_ a, decltype(*a)* dummy = 0){}

int main()
{
crasher(0);
return 0;
}

decltype(*a)*用于执行 T_成为类似指针的类型 - 例如 char* , int* , 和 shared_ptr<int> .

为什么会崩溃?这是已知错误吗?

最佳答案

假设你的目标是

decltype(*a)* used to enforce T_ to be a pointer-like type - such as char*, int*, and shared_ptr.

...你需要的是简单的模板,而不是碰巧使编译器崩溃的代码:)

这里有一些可能对你有用的东西

#include <memory>
#include <iostream>

// uncomment this "catch all" function to make select(0) compile
// int select(...){ return 0;}
template<class T> int select(T*){ return 1;}
template<class T> int select(std::auto_ptr<T>){ return 1;}
// add boost::shared_ptr etc, as necessary

int main()
{
std::cout << select(0) << std::endl;
std::cout << select(std::auto_ptr<int>()) << std::endl;
std::cout << select(&std::cout) << std::endl;
return 0;
}

关于c++ - 为什么这段代码会使 VC++ 编译器崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4344111/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com