Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/153.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++;同名的内联函数和外部函数会产生意外结果_C++_One Definition Rule - Fatal编程技术网

C++ C++;同名的内联函数和外部函数会产生意外结果

C++ C++;同名的内联函数和外部函数会产生意外结果,c++,one-definition-rule,C++,One Definition Rule,以下代码没有违反一条定义规则,但给出了意外的结果: 测试.hpp class Test { public: int test(); }; #include "Test.hpp" int Test::test() { return 1; } int test1() // expected to return 1 { Test a = Test(); return a.test(); } #include "Test.hpp" inline

以下代码没有违反一条定义规则,但给出了意外的结果:

测试.hpp

class Test
{
    public:
        int test();
};
#include "Test.hpp"

int Test::test()
{
    return 1;
}

int test1() // expected to return 1
{
    Test a = Test();
    return a.test();
}
#include "Test.hpp"

inline int Test::test() // doesn't violate ODR
{
    return 99;
}

int test2() // expected to return 99
{
    Test a = Test();
    return a.test();
}
#include <iostream>

int test1();
int test2();

int main()
{
    std::cout << test1() << std::endl;
    std::cout << test2() << std::endl;
}
Test1.cpp

class Test
{
    public:
        int test();
};
#include "Test.hpp"

int Test::test()
{
    return 1;
}

int test1() // expected to return 1
{
    Test a = Test();
    return a.test();
}
#include "Test.hpp"

inline int Test::test() // doesn't violate ODR
{
    return 99;
}

int test2() // expected to return 99
{
    Test a = Test();
    return a.test();
}
#include <iostream>

int test1();
int test2();

int main()
{
    std::cout << test1() << std::endl;
    std::cout << test2() << std::endl;
}
Test2.cpp

class Test
{
    public:
        int test();
};
#include "Test.hpp"

int Test::test()
{
    return 1;
}

int test1() // expected to return 1
{
    Test a = Test();
    return a.test();
}
#include "Test.hpp"

inline int Test::test() // doesn't violate ODR
{
    return 99;
}

int test2() // expected to return 99
{
    Test a = Test();
    return a.test();
}
#include <iostream>

int test1();
int test2();

int main()
{
    std::cout << test1() << std::endl;
    std::cout << test2() << std::endl;
}
main.cpp

class Test
{
    public:
        int test();
};
#include "Test.hpp"

int Test::test()
{
    return 1;
}

int test1() // expected to return 1
{
    Test a = Test();
    return a.test();
}
#include "Test.hpp"

inline int Test::test() // doesn't violate ODR
{
    return 99;
}

int test2() // expected to return 99
{
    Test a = Test();
    return a.test();
}
#include <iostream>

int test1();
int test2();

int main()
{
    std::cout << test1() << std::endl;
    std::cout << test2() << std::endl;
}
#包括
int test1();
int test2();
int main()
{

std::cout不允许将函数同时定义为
内联
和非
内联

如果使用了带有外部悬挂机构的功能 在一个翻译单元中声明内联,则应在其出现的所有翻译单元中声明内联; 无需诊断


([dcl.fct.spec]/4)

不允许将函数同时定义为
内联
和非
内联

如果使用了带有外部悬挂机构的功能 在一个翻译单元中声明内联,则应在其出现的所有翻译单元中声明内联; 无需诊断


([DC.FCT.SPEC]/4)

<代码>关于两个测试定义::测试,因为其中一个是内联定义,它不违反ODR < /代码>。你在哪里得到这个想法?好,我认为它不违反ODR,因为这个问题在C++规范中的ODR部分没有提到。“如果一个具有外部链接的函数在一个翻译单元中声明为内联,则该函数应在其出现的所有翻译单元中声明为内联;无需诊断。在函数说明符部分中提到,这与ODR。<代码>有关两个测试定义::测试,因为其中一个是内联定义,它不违反ODR < /代码>。您在哪里得到这个想法?好,我认为它不违反ODR,因为这个问题在C++规范中的ODR部分没有提到。“如果一个具有外部链接的函数在一个翻译单元中声明为内联,则该函数应在其出现的所有翻译单元中声明为内联;无需诊断。“是在函数说明符部分中提到的,它不涉及ODR。谢谢你的回答。但是你提到的语句没有在规范的ODR部分中说明。那么我可以说我提供的代码没有违反ODR吗?(它违反函数说明符规则,但不是ODR)@嘘,我不能说它是否有。ODR的措辞有“非内联函数”。因此,它隐式地假设函数不能同时是内联函数和非内联函数;它必须是一个或另一个。当您尝试声明内联函数和非内联函数时,ODR甚至没有意义。谢谢您的回答。但是您提到的语句没有在规范的ODR部分中说明。因此,我可以说我提供的代码没有违反ODR?(它违反了函数说明符规则,但不是ODR)@SHH我不能说它是否违反。ODR的措辞有“非内联函数”因此,它隐式地假定函数不能同时是内联函数和非内联函数;它必须是其中之一。当您尝试将同一函数声明为内联函数和非内联函数时,ODR甚至没有意义。