Error: getenv cannot be interpreted at compile time
Adam D. Ruppe via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Feb 17 11:29:51 PST 2015
On Tuesday, 17 February 2015 at 19:17:42 UTC, Paul wrote:
> I don't understand the error and Google doesn't help - can it
> be fixed or am I just using the wrong approach?
Trying to create it as a global tries to make it as a static
variable which means it constructs at compile time. It can't
access the TERM environment variable at compile time and that
causes the error.
Putting Terminal at global scope isn't really ideal, there'd be
potential destructor problems (the terminal needs to be cleaned
up at program termination). You could stick a pointer to it up
there though:
Terminal* terminal; // maybe make it shared too but i'd try to
avoid that, terminal isn't quite thread safe
void main() {
auto main_terminal = Terminal(options...);
terminal = &main_terminal;
scope(exit) terminal = null; // clean up the pointer too when
it goes invalid
// the rest of your program goes normally
// and other functions can use the global pointer
}
Just make sure you set it in main before doing anything else so
the pointer isn't null and you should be good to go. Then when
main returns, it will clean up.
More information about the Digitalmars-d-learn
mailing list