Issue
Is there any way to prevent mixing two different alias type that actually refer to the same underlying type in c++ ?
For instance, I would like to get a compilation warning with g++
for the following (although valid) program:
using TypeA = float;
using TypeB = float;
void foo(TypeA a) {}
int main()
{
TypeB b;
foo(b); // valid but how to get a warning?
return 0;
}
Unfortunately neither of -Wall
, -Wextra
or -pedantic
warn on this.
I'm looking for a solution that works with alias type (where I don't have to create two classes TypeA
and TypeB
).
Solution
No, it shouldn't be possible. Aliases are intended to not be a types, but an "aliases", synonyms. This means that they are intended to not be distingushable from the type they're aliasing. Type aliases don't have their own identity.
Mentioned in comments BOOST_STRONG_TYPEDEF in fact creates new type, which can be than distinguished from other types during function overloading etc. by compiler.
Read more:
- http://en.cppreference.com/w/cpp/language/type_alias#Explanation
- http://eel.is/c++draft/dcl.typedef
- http://www.boost.org/doc/libs/1_66_0/boost/serialization/strong_typedef.hpp
Answered By - Michał Łoś Answer Checked By - Marie Seifert (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.