English 中文(简体)
• 如何处理正在装入习惯集装箱的无效数据类型
原标题:How to handle invalid data type being inserted into custom container

我有一个我目前正在测试的定制清单集装箱。 我的一项测试是首先建造集装箱,以接受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!

最佳回答

问题是,<代码>float已经转换成uint8_t,因此,如果没有编纂时的解决办法,你就无法核对。

如果您正在使用C++17或更多,请您利用<条码>constexpr>:

template <typename T>
void libdsa::libstructures::LinkedList<T>::append(T datum)
{
    if constexpr (!std::is_same_v<T, uint8_t>)
    {
        std::cout << "This is an invalid type " << datum << "
";
        return;  // or throw an exception
    }
    //...
}

如果该类型不是uint8_t,if即为true并将执行该代码。

这里是


如果你想要停止汇编工作,如果发出错误类型,则根本不产生可起诉性,你可使用static_assert<>code>。 (与C++11合作):

#include <cstdint>
//... 
template <typename T>
void libdsa::libstructures::LinkedList<T>::append(T datum)
{
    // This will stop the compilation if the wrong type is sent   
    static_assert(std::is_same_v<T, uint8_t>, "It is not a uint8_t");
    //...
}

http://godbolt.org/z/EfhzGE7E5“rel=“noestlow noreferer”

问题回答

暂无回答




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?