PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, July 17, 2022

[FIXED] How to prevent combination of different alias type in c++?

 July 17, 2022     alias, c++, compilation, g++, warnings     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing