4

Somewhat similar to this post, but still different: can I define a global auto variable in some header file? I tried using the following files, but couldn't make them compile.

$ cat main.cpp
auto a = 5;
#include "defs.h"
int main(int argc, char **argv){ return a; }
$ cat defs.h
#ifndef __DEFS_H__
#define __DEFS_H__
extern auto a;
#endif

And after standard compilation (g++ main.cpp -o main) I got the following error:

In file included from main.cpp:2:0:
defs.h:3:8: error: declaration of ‘auto a’ has no initializer
 extern auto a;
        ^~~~

Is there any way to define a global auto variable in the source file and include it in some header file? Or will I have to give up this dream and find its type?

OrenIshShalom
  • 5,974
  • 9
  • 37
  • 87
  • and if you're interested in how the standard covers this : [Does a declaration using “auto” match an extern declaration that uses a concrete type specifier?](https://stackoverflow.com/questions/26386010/does-a-declaration-using-auto-match-an-extern-declaration-that-uses-a-concrete) – Sander De Dycker Sep 05 '19 at 12:34
  • 1
    `#define __DEFS_H__` This is a reserved identifier. By defining it, your program will have undefined behaviour. Choose another macro. – eerorika Sep 05 '19 at 12:37
  • 2
    Not related, but names such as `__DEFS_H__` are reserved in C++ (and C) - you should not be creating them in your own code. –  Sep 05 '19 at 12:37
  • just noticed that you referenced the duplicate in your question claiming yours is different - how is your question different ? – Sander De Dycker Sep 05 '19 at 12:38
  • @SanderDeDycker I guess the difference is that the solution presented there is "don't use `auto`". – Max Langhof Sep 05 '19 at 12:48
  • 1
    This is impossible; every translation unit that uses the variable would have to deduce the same type, and there isn't even any way to verify that they did. – molbdnilo Sep 05 '19 at 12:50
  • @MaxLanghof : whereas here it's "you can't use `auto`" - I guess there is a difference, but it's arguable if it matters. – Sander De Dycker Sep 05 '19 at 12:51

2 Answers2

3

Is there any way to define a global auto variable in the source file and include it in some header file?

You cannot declare auto variable without initialisation. With auto, the type is deduced from the initialiser. Without initialiser, the compiler cannot know the type. The compiler needs to know what the type is.

If you instead use the deduced type in the header, then following is technically allowed (according to SO post linked in the other answer) although it mostly defeats the purpose of using auto:

// header
extern int a;

// cpp
auto a = 5;

But unfortunately in practice, some compilers don't like this.

As a working alternative, you could simply use an inline variable:

// header
inline auto a = 5;

Pre-C++17, you'll need to give up the dream of auto for extern variables.

eerorika
  • 232,697
  • 12
  • 197
  • 326
2

According to the C++ 17 Standard (10.1.7.4 The auto specifier)

3 The type of a variable declared using auto or decltype(auto) is deduced from its initializer. This use is allowed in an initializing declaration (11.6) of a variable...

Si such a usage of the specifier auto

extern auto a;

is invalid.

You have to write

auto a = 5;

//...

extern int a;

Here is a demonstrative program

#include <iostream>

auto a = 5;

int main() 
{
    extern int a;

    std::cout << "a = " << a << '\n';
}

Take into account that it is a bad idea to define a variable in a header such a way. Starting from C++ 17 you could write

inline auto a = 5;
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335