我有一个我目前正在测试的定制清单集装箱。 我的一项测试是首先建造集装箱,以接受uint8_t型号,然后试图添加浮动型号并触发例外。 问题是,在我的附录职能中,浮标的附后没有触发我的例外情况。 当我打印数据时,浮标的实际内容没有显示,但随附清单改动后,数据似乎正在附上。
我怎么能够检查这些类型,这样我就把一个错误推向这一具体案件?
在此,我附上职能守则,并测试失败。
...
template <typename T>
void libdsa::libstructures::LinkedList<T>::append(T datum)
{
// Triggers even during a valid insertion.
// if (typeid(datum) != typeid(this))
// {
// throw std::runtime_error("Invalid type being inserted.");
// }
// Fill the head node if it s empty. Otherwise append a new node.
if (_head == nullptr)
{
try
{
_head = new libdsa::libstructures::Node<T>(datum);
_head->_next = _head;
_head->_prev = _head;
}
catch(const std::exception& e)
{
std::cerr << e.what() <<
;
}
}
else
{
try
{
libdsa::libstructures::Node<T> *current = _head;
// Create the new node
libdsa::libstructures::Node<T> *newNode = new libdsa::libstructures::Node<T>(datum);
// Go to the end of the list
while (current->_next != _head)
{
current = current->_next;
}
// Append the new node and set the directional pointers.
current->_next = newNode;
newNode->_prev = current;
newNode->_next = _head;
}
catch(const std::exception& e)
{
std::cerr << e.what() <<
;
}
}
++_size;
}
...
TEST(LinkedList, testInvalidAppend)
{
std::vector<uint8_t> data = { C , O , D , E };
auto list = setup(data);
float temp = 23.22;
list.append(temp);
list.print();
ASSERT_EQ(data.size(), list.getSize());
}
libdsa::libstructures::LinkedList<uint8_t> setup(std::vector<uint8_t> &data)
{
libdsa::libstructures::LinkedList<uint8_t> list;
for (size_t i = 0; i < data.size(); ++i)
{
list.append(data[i]);
}
return list;
}
在采取这种无效行动时,我就象一个例外(或时间——更长)被推翻,但是,在我提出有条件声明时,这两种情况都没有发生。
Initially, I attempted to us a try-catch block thinking that the compiler would catch it for me and print out the exception. However, it did not trigger it and allocated the data block. The other option I tried was to get the typeids of both the datum being inserted and the type of the container at runtime, but this caused an insertion failure in a successful case (where the type of the data and the container were actually the same).
Solution Update
Following the first method of PaulMcKenzie s answer, I was able to get it working so the template type T was compared to whatever was passed into the append function by the user, which became template type K. Below is the new function declaration with my container class and its definition.
...
template <typename T>
class LinkedList
{
public:
/// @brief Default constructor.
LinkedList() = default;
/// @brief Appends a new data instance to the end of the list.
/// @param datum Data instance to be appended.
template <typename K>
void append(K datum);
...
...
template <typename T>
template <typename K>
void libdsa::libstructures::LinkedList<T>::append(K datum)
{
if constexpr (!std::is_same_v<T, K>)
{
throw std::runtime_error("Invalid type being appended.");
}
...
This now allows the container to be any data type and for that type to be compared to the type of a data instance the user wants to insert. After testing his method with the updates, it appears append()
is now able to force the user to only insert data instances of the same type as the container. Many thanks to everyone for taking the time to help!