Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

making a typedef local to a file or subclasses

I'm consolidating 2 programs into one, and in 2 different files (I have many files), I have a typedef with the same name, different types though.

These types will be used in completely different parts of the program and will never talk to each other, or be used interachangely.

I can of cause just do a search replace in one of the files, but I was wondering if there is another solution to this.

Something like binding a typedef to a specific file. Or making a typedef local to a class and it's subclasses.

Thanks

like image 786
monkeyking Avatar asked Jun 14 '11 13:06

monkeyking


People also ask

Is typedef local?

It's legal and localized.

Where should you put typedef?

First way is to typedef at the place-of-first-declaration. Second way is to typedef at each place-of-use, and make it only visible to that place-of-use (by putting it inside the class or method that uses it).

What is the function of typedef in C++?

The typedef keyword allows the programmer to create new names for types such as int or, more commonly in C++, templated types--it literally stands for "type definition". Typedefs can be used both to provide more clarity to your code and to make it easier to make changes to the underlying data types that you use.

Does typedef create a new type?

Syntax. Note that a typedef declaration does not create types. It creates synonyms for existing types, or names for types that could be specified in other ways.


2 Answers

typedefs are always "local to a file". So it is not exactly clear what you mean by "making it local to a file". Typedef does not introduce an entity with its own linkage, it simply creates an alias to an existing type. For that reason the problem of "making it local to a file" simply does not exist. Each typedef is only visible in the translation unit (file) in which it is declared. So, if you can make sure that your identically named typedefs never meet each other in a common translation unit, you problem is formally solved.

It is not a good programming practice though to have the same typedef-name refer to different types in different files, unless these files are naturally separated somehow (like belong to different libraries, or something like that).

Otherwise, you can always rename one of the typedefs, or make it a class member or a namespace member. Keep in mind though that in general case the making a typedef member of a class will require virtually the same kind of effort as renaming it: the references to that typedef will have to be updated in every place in which they are present. Namespaces might be a bit easier, since with namespaces you can use using directive.

But again, if your typedefs are only referrd from two disjoint sets of files, then the problem does not formally exist. If there are files that are supposed to use both typedefs, then the effort you'll have to spend fixing these files will be equivalent to renaming the typedefs (regardless of the method you finally choose).

like image 149
AnT Avatar answered Oct 05 '22 23:10

AnT


You can encapsulate those typedef inside a namespace:

namespace N1 {
  typedef int T;
}
namespace N2 {
  typedef int T;
}

And in whatever file you want to use first typedef simply declare:

using namespace N1;

same thing for the other one also.

like image 26
iammilind Avatar answered Oct 05 '22 23:10

iammilind