4

auto is comfortable to use but sometimes, for a matter of legibility while programming I would prefer to have the inferred type written in the source code instead of auto.

Is there a functionality of gcc/clang or some other tool/VSCode extension ( for linux or macos ) that takes as input the source files and spits back the same source files but with auto replaced by its inferred type?

I tried googling but and the closest thing I've found is this StackOverflow question and it doesn't answer my question.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
Gerardo Zinno
  • 1,518
  • 1
  • 13
  • 35
  • 5
    A good IDE should show you what the type is if you hover with the mouse on it. – Enlico Jul 16 '20 at 15:45
  • @EnricoMariaDeAngelis VSCode does it, sometimes, but in situations like in #ifdef blocks it doesn't work if the if condition is not satisfied. – Gerardo Zinno Jul 16 '20 at 15:52
  • 1
    Compilers are usually not modifying the source code, this is the job of the editor, IDE or other editing tools. It seems for example ReSharper C++ (a Visual Studio extension) can do this: https://www.jetbrains.com/resharper-cpp/features/ > For example, you can quickly replace auto with explicit type or replace type with auto – Suma Jul 16 '20 at 16:43

2 Answers2

2

In combination with using clang as a compiler, you use clangd for auto-completion, … and it will show the type of auto on mouseover (as long as it does not depend on a template variable)

t.niese
  • 39,256
  • 9
  • 74
  • 101
1

In general this is not always possible. In the following case, the compiler cannot substitute auto with one type, because 3 different functions get actually instantiated.

template<typename T>
auto f(T x) {
    auto y = x;
    return y;
}

int main() {
    f(1);
    f('c');
    f("c");
}

Someone in the comments says that it should substitute auto for T, because that's actually what happens in my trivial example: auto is deduced to be the same type as T is deduced to be, which in my opinion just means that in this example auto is playing the same role as T, which is the "placeholder" (maybe this is not the right terminology) for the type the compiler will deduce at each call site. Even if you substitute auto for T (in this trivial example), what invermation are you getting more than you had? Nothing.

If the main above was instead

int main() {
    f(1);
}

then yes, you or the compiler could substitute auto with int (or whatever), but in the original code, there would be no single substitution that would make happy all three lines inside that main. The compile could just substitute the whole template function with a sequence of the various instantiations that it needs. And the number of those depends on how many different types are passed to f in the whole codebase that makes use of f.

If you write a library, with a nice template function in it, you cannot know which types it will be instatiated with, when I will use it in my code; so what are you substituting T or auto with?

Enlico
  • 23,259
  • 6
  • 48
  • 102
  • 4
    Some types (like lambdas) are unique and indescribable. – Suma Jul 16 '20 at 15:48
  • 1
    What should the compiler deduce for the `auto` in this code, in your opinion? Not to mention what @Suma just wrote. – Enlico Jul 16 '20 at 15:48
  • 2
    It's always possible, but often you would see the uglified internal type names, not the exposed interfaces. Resolving the canonical name for types is the actually tricky part. – Ext3h Jul 16 '20 at 15:48
  • Yes it is, what are you talking about? Template functions are no different, their code gets copied and adjusted for each type usage you have. At every single point you have hard, static types for every variable. – Blindy Jul 16 '20 at 15:49
  • @EnricoMariaDeAngelis Lambdas are better example, but I see your point - such a tool would be lacking as in "it sometimes works and sometimes not" – Yksisarvinen Jul 16 '20 at 15:51
  • @EnricoMariaDeAngelis In my opinion the tool I'm looking for could leave auto at the function definition and replace auto in situations like `auto c = f('c')` – Gerardo Zinno Jul 16 '20 at 15:51
  • 2
    Maybe this would be a better example: https://godbolt.org/z/W839zj – Alan Birtles Jul 16 '20 at 15:52
  • @Suma, is there any more information in saying that _the type of `y` is whatever `T` is deduced to be_ with respect to _the type of `y` is whatever `auto` is deduced to be_? They're just the same, they also use the same type deduction rules (except when braced initializers are concerned). – Enlico Jul 16 '20 at 16:01