Main Content

MISRA C++:2023 Rule 8.2.11

An argument passed via ellipsis shall have an appropriate type

Since R2024b

Description

Rule Definition

An argument passed via ellipsis shall have an appropriate type.

Rationale

Passing arguments of certain class types via ellipsis is only conditionally supported, subject to implementation-defined semantics.

For instance, if an argument of a class type with a nontrivial copy constructor is passed via ellipsis, the copy constructor might not be called. As a result, the copy semantics in the constructor might not be enforced properly.

Polyspace Implementation

Based on the MISRA™ C++:2023 specifications, the rule checker reports violations if an argument passed to a variadic function via ellipsis has a class type with one of the following:

  • Nontrivial copy or move operations

  • Nontrivial destructor

  • Virtual member functions

The checker allows passing of such arguments if the function call is not evaluated, for instance, when the function call is used as argument to one of sizeof, typeid, decltype or noexcept.

The rule violation is shown on the invocation of the variadic function. The result message indicates the reasons why the argument passed via ellipsis does not have an appropriate data type. The event list below the result message shows the location where the argument data type is defined.

Troubleshooting

If you expect a rule violation but Polyspace® does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

#include <cstring>

class dataClass {
public:
    char* data;
    dataClass(const char* text) {
        data = new char[std::strlen(text) + 1];
        std::strcpy(data, text);
    }
    // Non-trivial copy constructor for deep copy
    dataClass(const dataClass& other) {
        data = new char[std::strlen(other.data) + 1];
        std::strcpy(data, other.data);
    }
    ~dataClass() {
        delete[] data;
    }
};

// Variadic function using ellipsis
void processTextData(...) {
}

void handleTextData() {
    dataClass textData("Text data");
    processTextData(textData); // Noncompliant
}

In this example, the variable textData has a class type with a nontrivial copy constructor. Therefore, passing this variable to a variadic function processTextData via ellipsis violates the rule.

Check Information

Group: Expressions
Category: Advisory

Version History

Introduced in R2024b