Skip to content

Header function definition should be marked inline to avoid ODR violations #1

@StonerLing

Description

@StonerLing

Hi,

这是关于2.1 传统代码文件风格 示例的一点小建议

// mcpplibs.hpp
#ifndef MCPPLIBS_HPP
#define MCPPLIBS_HPP

int add(int a, int b) {
    return a + b;
}

#endif

头文件里直接定义普通函数的写法,在被多个 .cpp 包含时可能会导致链接阶段的 multiple definition / ODR violation 问题。建议添加inline关键字:

inline int add(int a, int b) {
    return a + b;
}

另外感慨一句,刚了解到这个社区,特地来支持一下,虽然上班基本不可能用到这么新的 C++ 哈哈

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions