Is this a name collision?

I was testing template specialization and below is fine
1
2
3
4
5
template<typename T>
void is_voi(T input){};

template<>
void is_voi <char> (char input){};


But this will complain:
expected initializer before ‘<’ token
1
2
3
4
5
template<typename T>
void is_void(T input){};

template<>
void is_void <char> (char input){};


Is this a naming issue?
Last edited on
I think so. The latter seems to work if I don't use using namespace std;.
If there is using namespace std; in your code, the second snippet is a name collision.

https://en.cppreference.com/w/cpp/types/is_void

The C++ library template returns a value, either true or false.
Here is the list of all the std symbols:

https://en.cppreference.com/w/cpp/symbol_index

But it is much better if one puts stuff into it's own namespaces to avoid these conflicts to start with.
But it is much better if one puts stuff into it's own namespaces to avoid these conflicts to start with.

Something that isn't taught in most programming courses, it seems.

I learned to create custom namespaces the hard way, lots and lots of name collisions 'cuz I liked using names already used in either the C and/or C++ library.
Something that isn't taught in most programming courses


The nearest that is probably mentioned re namespaces is to tell the students to put:

 
using namespace std;


after the includes to make the code compile!

See:
https://www.learncpp.com/cpp-tutorial/naming-collisions-and-an-introduction-to-namespaces/
https://www.learncpp.com/cpp-tutorial/using-declarations-and-using-directives/

std::is_void() is defined by the standard since C++11
https://en.cppreference.com/w/cpp/types/is_void

Last edited on
What really fries me about the use and over-use abuse of having using namespace std; liberally dosed in one's code is even Saint Bjarne, the Pope of C++, uses it in code used in his Uni classes. I see it all over his "Programming Principles and Practice Using C++ - 2nd Edition" book.
> Saint Bjarne, the Pope of C++, uses it

He is not alone; many respected names in the C++ community tend to concur with him.

For example, Alexandrescu and Sutter in C++ Coding Standards:
In short: You can and should use namespace using declarations and directives liberally in your implementation files after #include directives and feel good about it. Despite repeated assertions to the contrary, namespace using declarations and directives are not evil and they do not defeat the purpose of namespaces. Rather, they are what make namespaces usable.


There are two sides to this debate:
https://www.cplusplus.com/forum/beginner/275318/#msg1188303
It's a tricky one. Beginners at C++ (who are, often, beginners to programming entirely), have lots and lots of things to try and understand very quickly, and it seems reasonable to me that both teachers and students would benefit from not having to explain/learn what namespaces are, what std:: is, and how they should be used, when students are still struggling to learn the rudiments.

I don't think there's any harm in telling students that, for now, simply adding that bit of boilerplate to their code to simplify the use of standard library elements, on the understanding that it's not good practise in "real code" and will be unlearned at some point.

As experienced - and, in many cases, professional - C++ coders, we come at this from a very different perspective from people struggling to learn programming from the very beginning, and we often forget to think about things from those students' perspective - or from the teachers'.

Bringing the ENTIRE namespace in globally defeats the purpose of having namespaces. That is what we commonly see.

Stroustrup puts using namespace std; into frikkin' header files he provides for students to use!

Bringing in discrete chunks in limited scope, using std::cout; for example, mutes the insanity.

I personally avoid using namespace std;, period. No matter where. It is just plain lazy and a bomb waiting to go off.

Rarely I might use a more restrictive form, using namespace std::ranges::views; at a localized scope. Never in a header file or the start of an implementation file. Sanity prevails.

I consider using namespace std; as bad and sloppy coding the way it is used by many people. "I'm too lazy to type a fully qualified name!"

I never said it was evil.
I don't think there's any harm in telling students that, for now, simply adding that bit of boilerplate to their code to simplify the use of standard library elements, on the understanding that it's not good practise in "real code" and will be unlearned at some point.


Yes - but that 'some point' doesn't seem to come in many cases...

I confess I use:

1
2
using namespace std::string_literals;
using namespace std::string_view_literals;


so that the s and sv can be applied to string literal and it's awful without.
Now that type of using I use (*groan*) when it can make things easier, seeplus. You aren't bringing in the ENTIRE damned namespace, only the bits and pieces needed.

You know why you do something.

The main problem with "tell students it's OK to use something, hoping it will be unlearned later" just doesn't happen except rarely.
> hoping it will be unlearned later

There is no universal concurrence that it is something that definitely has to be unlearned later.
Repeat: https://www.cplusplus.com/forum/beginner/275318/#msg1188303

You have your personal stylistic preference; it is my preference too. But I recognise that it is no more than that: it remains just an opinion, even if one gets emotional about it.
Yes - but that 'some point' doesn't seem to come in many cases...

That's true, but then I'd guess that 99.9% of those cases are going to be for people who never actually go on to do any coding other than exercises for school, so it doesn't really matter.

I'd guess that most people who continue to code to the point where they're getting their hands on production code, will most likely have continued learning C++ at a level where they'll have been taught properly about namespaces, and why putting "using" in headers is a terrible idea.

Anyway, my point generally is that when you're teaching people at the very start of their education on a particular subjects, there are some shortcuts you have to take regarding details, in order to keep things simple enough for people to grasp the rudiments you need.

Just as it's OK to start kids out on Newtonian physics as an approximate model, and save quantum physics until much later, I'm also OK with telling kids to just write using namespace std; when they're starting, and then getting them to unlearn it later.

Putting it into a header file, though, is a step too far ;)

EDIT: And this is probably way more of a digression than the OP wanted!
Last edited on
there are some shortcuts you have to take regarding details


Yes - like #include etc. But IMO using namespace std at the beginning is not one of them. Just teach them to use std::cout, std::cin etc straight away and then at the right point cover namespaces, explain why std:: is used, then cover using etc. This way they get to start with/learn good practice first.
Putting it into a header file, though, is a step too far ;)

Yet that is something Sroustrup does with using namespace std; for his college students learning to code. Mentions it is not a good idea to have the using directive statement, especially in a header, then proceeds to do it for quite a chunk of his lecture code. In a header file that gets reused again and again.

No matter who the expert is, that is considered to be one of the the biggest taboos one shouldn't do.

There are instances where having using declarations, placed for as limited a scope as possible, do make sense in managing the rather ad-hoc nested namespace insanity of some parts of the C++ library to be less frustrating.

Boost can be equally "wordy" in burying things deep in multi-layered nested namespaces.

Might as well have beginning math students learn "2 + 2 = 5, no need to be TOO pedantic."
Last edited on
Topic archived. No new replies allowed.