How to avoid std namespace in header!.

Is there any way to avoid the std namespace in the header?.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#ifndef ADDTUPLE_H
#define ADDTUPLE_H
#include <tuple>

class AddTuple {
	public:
		std::tuple<double, char> get_student(int);
};
#endif


#include "AddTuple.h"
#include <iostream>
#include <stdexcept>
std::tuple<double, char> AddTuple :: get_student(int id)
{
	if (id == 0) return std::make_tuple(3.8, 'A');
	if (id == 1) return std::make_tuple(2.9, 'C');
	if (id == 2) return std::make_tuple(1.7, 'D');
	throw std::invalid_argument("id");
}

int main()
{
	double gpa1;
	char grade1;
	std::string name1;
	AddTuple addTpp;
	std::tie(gpa1, grade1) = addTpp.get_student(1);
	std::cout << "ID: 1, "
		<< "GPA: " << gpa1 << ", "
		<< "grade: " << grade1 << '\n';
	return 0;
}
Well, if you want to avoid std:: (not sure why you want to do that?) then create a struct with elements of type double and char and refer to that instead.

If you use tuple then you are referring to something in the std:: namespace so "std" is going to appear somehow.
I think you're misunderstanding the guideline, sort of like a game of "telephone".

The guideline is, don't use "using namespace X;" in the global scope of a header file, because it pollutes the global namespace for anyone using the header.
Last edited on
Thanks Ganado and lastchance,
Well, Generally in the header file, it is not recommend to use a namespace. But I understood Ganado's explanation. Thanks.
Well, Generally in the header file, it is not recommend to use a namespace.

No it is perfectly acceptable to use namespaces in header files, however using "using" statements in the global namespace in a header is considered a bad practice.
Thanks jlb
Topic archived. No new replies allowed.