Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在编译时断言函数是特定类的成员_C++_Assert - Fatal编程技术网

C++ 如何在编译时断言函数是特定类的成员

C++ 如何在编译时断言函数是特定类的成员,c++,assert,C++,Assert,我有一个界面,其中每个函数的内容都是用一个大宏创建的。如果程序员正在添加一个新函数,而忘记将该函数添加到接口类中,则会产生许多编译错误,从而分散了对实际错误的注意力 是否可以在编译时断言使用此特定宏的函数是特定类的成员?C++03或Boost功能可用 #define MACRO_OF_THE_DOOM(...) assertion_here(); do_something(); class A { void functionA(); void functionB(); };

我有一个界面,其中每个函数的内容都是用一个大宏创建的。如果程序员正在添加一个新函数,而忘记将该函数添加到接口类中,则会产生许多编译错误,从而分散了对实际错误的注意力

是否可以在编译时断言使用此特定宏的函数是特定类的成员?C++03或Boost功能可用

#define MACRO_OF_THE_DOOM(...) assertion_here(); do_something();

class A {
    void functionA();
    void functionB();
};

// This is valid usage
void A::functionA() {
    MACRO_OF_THE_DOOM(1, 2, 3, 4, 5);
}

// This should give an understandable compile error, which tells
// definition should be A::functionB()
void functionB() {
    MACRO_OF_THE_DOOM(6, 7, 8);
}
你可以用

关于这一点,很少有可以使用type_traits解决的注意事项,但这种解决方案可能适用于许多情况

是否可以在编译时断言使用此特定宏的函数是特定类的成员

如果您可以使用boost(我知道您不能使用c++11),那么我建议。下面是带有注释的示例:

#包括
#包括
#包括
BOOST\u TTI\u具有成员功能(FUNCTION)
BOOST\u TTI\u具有成员功能(功能B)
甲级{
public://必须是tti的public
无效函数();
//void函数b();
};    
int main()
{   
//印刷品1
std::cout::value//参数列表

因此,这样做的真正原因是,在实现函数时,您总是忘记在函数前面加上
A::
?您是否尝试过
-Wall
?@BartekBanachewicz我认为在这种情况下,静态断言比
-Wall
更好,因为编译错误显示在编译器警告之前。我还对whet感兴趣她说,这在理论上可能与静态断言有关。@BartekBanachewicz此外,我使用的编译器似乎没有针对此类问题的警告。您正在寻找比
#define MAKRO_of_DOOM(p1,p2,p3)/**更复杂的东西,以防出现错误:检查该函数是否是Foo**/((Foo*)this)的成员->someOtherFun();
?@VolkerK Yes;在您的示例中,编译错误将显示在使用宏的行中。程序员永远不会读取宏的定义(并查看注释)。因此,有一条可理解的错误消息是有益的。
#define MACRO_OF_THE_DOOM(...)  { assertion_here(); do_something(); }

assertion_here() { BOOST_STATIC_ASSERT(false); }
class A {
    assertion_here() { // no-op }
    void functionA();
    void functionB();
};
#include <iostream>

#include <boost/tti/has_member_function.hpp>
#include <boost/static_assert.hpp>

BOOST_TTI_HAS_MEMBER_FUNCTION(functionA)
BOOST_TTI_HAS_MEMBER_FUNCTION(functionB)

class A {
public: // must be public for tti
    void functionA();
    //void functionB();
};    

int main()
{   
    // prints 1
    std::cout << has_member_function_functionA<
       A, // class type to check
       void,    // function return type
       boost::mpl::vector<> >::value // parameter list
       << std::endl;

    // Below generates no compile error - prints 0
    std::cout << has_member_function_functionB<
       A, // class type to check
       void,    // function return type
       boost::mpl::vector<> >::value // parameter list
       << std::endl;

    // Below static assertion, will fail at compile time    
    BOOST_STATIC_ASSERT(
        (has_member_function_functionB<A,void,boost::mpl::vector<> >::value));

}
main.cpp: In function 'int main()':
main.cpp:32:5: error: invalid application of 'sizeof' to incomplete type 'boost::STATIC_ASSERTION_FAILURE<false>'
     BOOST_STATIC_ASSERT(
     ^
main.cpp:32:5: error: template argument 1 is invalid
     BOOST_STATIC_ASSERT(
     ^