VS2017 版本 15.8.3 成功编译内联方法,而不返回所需值
VS2017 version 15.8.3 successfully compiles inline method without returning a required value
我用VS2017版本15.8.3编译了以下代码。其警告级别设置为/W4
。该代码包含两个简单的 getter,其中一个GetM()
内联的。
GetM()
内联 getter 没有return
语句。但是,VS2017愉快地编译了代码,没有任何警告或错误。
如果GetN()
方法的return n;
语句被注释掉,则会导致error C4716: 'Simple::GetN': must return a value
。
class Simple
{
int m = 0;
int n = 0;
public:
int GetM() const { /* No return here. */ }
int GetN() const;
};
int Simple::GetN() const
{
return n;
// No return here results in compiler error below.
// error C4716: 'Simple::GetN': must return a value
}
int main()
{
Simple obj;
}
问题:编译器是否也应该为内联方法GetM()
生成error C4716
?
类定义中完全定义的方法是一种内联的。如果未直接在使用它的位置内联扩展,则会在类主体外部编译。这是一个神奇的调味料,它允许方法查看类中在其之后定义的任何成员。
如果不使用它,编译器可能没有足够深入地查看它以发现错误。也许它根本不看它。也许它会生成警告,也许不会。这取决于编译器。Visual Studio似乎已选择将缺少的返回语句报告为错误,但不检查未使用的inline
(或内联(函数。
通过将main
更改为
int main()
{
Simple obj;
obj.GetM();
}
我可以让Visual Studio为gGetM
生成错误C4716,因为现在必须编译该函数,无论是否内联。
我也可以
inline int Simple::GetN() const
{
}
明确GetN
inline
并"消除"错误。
这都是高度编译器,甚至可能是编译器选项,特定于。
相关文章:
- 从父类方法返回子类对象
- 获取从C++中同一类中的构造函数调用的方法返回的值
- 让bool方法返回其他整数
- 不能将方法返回的值用于另一个方法
- 如何实现 Front() 方法以返回模板化双向链表C++的第一个元素?
- 从私有成员变量的成员方法返回unique_ptr
- 我的模板类方法返回错误类型?
- std::find,返回所有找到的值的替代方法,而不仅仅是存在重复的向量的第一个值
- QtQuick - qml:28:错误:未知方法返回类型:自定义类型
- 方法错误"not all control paths return a value"和方法不返回值
- GRPC C++ TLS 客户端 grpc::SslCredentials() 方法不返回
- 我无法使用C++指针指向类方法返回的 std::vector
- 如何在工厂方法中返回指向基于基础操作系统的派生类的有效指针
- 将传入的网络"char*"数据转换为"uint8_t"并返回的安全方法是什么?
- C++方法是否可以根据传递给构造函数的参数具有不同的返回类型?
- EnQueue 方法在循环队列中未正确返回C++?
- 应用于const和非const对象的引用返回方法
- 在vc++ PPL中,如何创建一个同步返回的任务返回方法
- 为什么从浮点返回方法返回双精度不会在 c++ 中导致任何错误/警告
- 这个返回方法叫什么?