Is there any way to add functions to std?

I have just been very curious as to how header files add functions or anything at all to std because i have tried all the following below but none of them work
1
2
3
void std::test(){}
void std:test1(){}
void std.test2(){}

Can someone help me plz, the reason i want to know is because it just would be good to know how this stuff works
You use namespace.
1
2
3
4
5
6
7
8
9
10
#include<iostream>

namespace std {
    int yodawg = 42;
};

int main()
{
    std::cout << std::yodawg << std::endl;
}


But you'd never add anything to std, you'd pick your own meaningful namespace for your own work.

Adding things to std:: is walking into a minefield. Sooner or later, you're going to be having a really bad day.
The standard library implementation does this:
namespace std { void test() {} }

As a user of the C++ implementation, it is incorrect to add things to namespace std, excepting specializations of many templates that depend on a user defined type - for example, std::hash<my_type>. So if your program contained the above, it would be wrong. Only the standard library can legally add functions to namespace std.
Last edited on
Topic archived. No new replies allowed.