英文:
Can you properly test a const member function If you include exception safety inside that function without having to comment it out after?
问题
以下是您要翻译的内容:
问题
如何测试一个常量成员函数?
目标:
单元测试一个常量下标运算符并获得通过的测试。换句话说,我是否可以避免编写一个测试;让编译器抛出错误来证明成功,然后避免需要注释掉测试以允许其余测试正确编译?
我的尝试:
我的推理是,如果我测试is_const<>::value并且为真,我可以抛出一个异常,该异常将被我的单元测试捕获,并允许测试“通过”,因为未能更改下标索引位置的元素值。
我的常量下标定义
#include <type_traits>
// operator [] const
template <typename T>
typename Vector<T>::const_reference
Vector<T>::operator[](size_t index) const {
if (size_ <= index || index < 0) {
throw std::out_of_range("Index out of range.");
}
if constexpr(std::is_const<std::decltype(*this)>::value) {
throw std::runtime_error("Cannot modify elements of a const Vector.");
}
return array_[index];
}
我的单元测试
TEST(SubScriptConst) {
const Vector<int> myVecConst = {0,1,2,3,4,5,6,7,8};
try {
myVecConst[2] = 4; // 这将抛出一个异常?
}
catch (const std::exception& ex) {
std::cout << "成功 | 捕获到异常:" << ex.what() << std::endl;
}
CHECK_EQUAL(myVecConst[2], 2);
}
显而易见。
它不起作用。我知道这是因为我对语言的理解不够。我的假设是编译器在代码执行之前检查常量的值赋值并报错。如果可能的话,我将把try块放在一个用于测试的宏#ifdef中,仍然允许编译器在不测试时抛出错误。
英文:
Question
How does one test a const member function?
Goal:
Unit test a const subscript operator and get a passing test. In other words can I avoid writing a test; have a compiler throw an error to prove success, then avoid the need to comment the test out to allow proper compilation for the remainder of the tests?
My Attempts:
My reasoning was if I test for is_const<>::value and is true I can throw an exception that would be caught by my unit test and allow the test to "Pass" do to failure to change value of element at location of subscript index.
my const subscript definition
#include <type_traits>
// operator [] const
template <typename T>
typename Vector<T>::const_reference
Vector<T>::operator[](size_t index) const {
if (size_ <= index || index < 0) {
throw std::out_of_range("Index out of range.");
}
if constexpr(std::is_const<std::decltype(*this)>::value) {
throw std::runtime_error("Cannot modify elements of a const Vector.");
}
return array_[index];
}
my UnitTest
TEST(SubScriptConst) {
const Vector<int> myVecConst = {0,1,2,3,4,5,6,7,8};
try {
myVecConst[2] = 4; // This will throw an exception?
}
catch (const std::exception& ex) {
std::cout << "SUCCESS | Caught exception: " << ex.what() << std::endl;
}
CHECK_EQUAL(myVecConst[2], 2);
}
The obvious.
It does not work. I know its because of my lack of understanding the language. My assumptions are the compiler is checking the value assignment of a const before the code is executed and gives error. If I am able to I will be placing the try block inside a MACRO #ifdef for testing only and still allow the compiler to throw error when not testing.
答案1
得分: 1
如果一个类的成员函数被声明为const
,那么它不能修改任何类数据成员,除非这些类数据成员被声明为mutable
。
这种const
行为是由编译器强制执行的,而不是由C++运行时执行的。因此,如果有人尝试在const
成员函数中修改非mutable
数据成员,那将是编译时错误,而不是运行时错误。因此,您可以跳过编写这种测试用例。
以下是C++标准中的一些相关摘录:
[注 1:因此,在
const
成员函数中,调用该函数的对象是通过const
访问路径访问的。 —结束注释]
int s::f() const { return a; }
在
s::h
的主体中的a++
是非法的,因为它试图修改调用s::h()
的对象(部分)。这在const
成员函数中是不允许的,因为this
是一个指向const
的指针;也就是说,*this
具有const
类型。
英文:
If a class member function declered as const
then it can't modify any class data members except the class data members which are declered mutable
.
This const
behavior is enforced by compiler and not the C++ runtime. So if someone will try to modify a non-mutable data member in const
member function then it will be a compile time error and not a runtime error. So you can skip writing such test cases.
Following are some related excerpts from in C++ standard:
>[Note 1: Thus in a const
member function, the object for which the function is called is accessed through a const
access path. —end note]
>
>
>int s::f() const { return a; }
>
>The a++
in the body of s::h
is ill-formed because it tries to modify (a part of) the object for which s::h()
is called.
>
>This is not allowed in a const
member function because this
is a pointer to const
; that is, *this
has const
type.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论