Outpost Universe Forums

Projects & Development => Outpost 2 Programming & Development => Topic started by: Vagabond on June 07, 2017, 11:24:11 PM

Title: Help adding a static, compile time constant variable to a class/struct in C++
Post by: Vagabond on June 07, 2017, 11:24:11 PM
I’m trying to add a private variable to a class that is constant at compile time and static (does not reside in an individual instance of the class.)

Unfortunately, I cannot get my code to compile. Below is roughly what I’m typing in.
 
Code: [Select]
class XFile
{

...

private:
constexpr static string notSupportedMessage("Operating system not supported.");
};
I’m getting error code E0079 Expected a Type Specifier.

I was hoping for some insight in how to properly format the code.

-Brett

Title: Re: Help adding a static, compile time constant variable to a class/struct in C++
Post by: Sirbomber on June 08, 2017, 08:04:38 PM
String isn't a built-in type, so I don't think you can do what you're trying to do.  You can probably declare it as static and then define it elsewhere, though.  However, I also think your code is wrong for what you say you want to do - you say you want a variable, but isn't this defining a function named notSupportedMessage that returns a string and takes the argument "Operating system not supported."?
Title: Re: Help adding a static, compile time constant variable to a class/struct in C++
Post by: Vagabond on June 09, 2017, 10:50:20 PM
Okay thanks for taking a look at it sirbomber.

I think my code should be calling the constructor on std::string and setting it to "Operating system not supported.". I think this weekend I should be able to put some more time into it.
Title: Re: Help adding a static, compile time constant variable to a class/struct in C++
Post by: Hooman on June 10, 2017, 09:30:12 AM
You declare static variables inside the class definition, but define static variables outside of the class definition.

Code: [Select]
// XFile.h
class XFile {
private:
  static string notSupportedMessage;
};

// XFile.cpp
string XFile::notSupportedMessage("Operating system not supported.");

Though, considering your message, I wonder if a compile time error might be more appropriate?
Title: Re: Help adding a static, compile time constant variable to a class/struct in C++
Post by: Arklon on June 10, 2017, 06:10:36 PM
Yeah, this sounds like something you'd want to just use a static_assert for.
Title: Re: Help adding a static, compile time constant variable to a class/struct in C++
Post by: Vagabond on June 11, 2017, 12:34:38 AM
Thanks Hooman & Arklon,

I wasn't aware of the preprocessor directive #error, which works much better than throwing an error when the code executes in this case.

Thanks for the help!

-Brett
Title: Re: Help adding a static, compile time constant variable to a class/struct in C++
Post by: Hooman on June 11, 2017, 01:34:11 AM
Edit: Got sidetracked while composing this response. Seems you beat me to #error.

Using static_assert (http://en.cppreference.com/w/cpp/language/static_assert) could work. You can also use #error (http://en.cppreference.com/w/cpp/preprocessor/error).

Example:
Code: [Select]
#ifdef _WIN32
  // Windows specific code
#elif __linux__
  // Linux specific code
#else
  #error OS not supported
#endif

As per the linked documentation, it doesn't matter if the string after #error is quoted or not.

To use static_assert, instead of #error, it might look something like this:
Code: [Select]
static_assert(false, "OS not supported");

Note: You can't simple use preprocessor #defines such as _WIN32, or __linux__ directly with static_assert, since if they're not defined (normally checked by #ifdef/#ifndef), there is no symbol substitution, so the compiler will see an unknown symbol. Also, to get this to compile under Linux, the newer version of C++ must be explicitly enabled: g++ -std=c++11 cppError.cpp
Title: Re: Help adding a static, compile time constant variable to a class/struct in C++
Post by: Arklon on June 11, 2017, 02:33:04 PM
The problem with #error is MSVC's syntax for it uses no quotes and GCC's syntax for it does.
Title: Re: Help adding a static, compile time constant variable to a class/struct in C++
Post by: Hooman on June 25, 2017, 01:32:01 PM
Do you have a reference for that? I just went looking to confirm, and couldn't find anything. I did however see a few GCC examples that didn't use quotes.

Admittedly, it looks way better with quotes.
Title: Re: Help adding a static, compile time constant variable to a class/struct in C++
Post by: Vagabond on June 25, 2017, 02:09:56 PM
Just tested on Visual Studio 2017. MSVC compiles and runs both with or without quotes. However, if you put quotes around the message, the error message will include the quotes. Ideally, the quotes wouldn't be there, but it compiles and gets the message across clearly.

If someone wants to test on GCC we could compare.

Code: [Select]
#if defined (linux)

    . . .

#else
#error "Operating system not supported."
#endif

Results
Code: [Select]
Error (active)	E0035	#error directive: "Operating system not supported." OP2Utility
Code: [Select]
Error	C1189	#error:  "Operating system not supported." OP2Utility
Title: Re: Help adding a static, compile time constant variable to a class/struct in C++
Post by: Hooman on June 26, 2017, 02:27:12 AM
Tested with GCC:
Code: [Select]
#error OS not supported

Result:
Code: [Select]
cppError.cpp:5:2: error: #error OS not supported
 #error OS not supported
  ^
Title: Re: Help adding a static, compile time constant variable to a class/struct in C++
Post by: Vagabond on June 29, 2017, 06:13:13 PM
@Hooman,

So this means the GCC compiler supports #error without quotes around the error message?

-Brett
Title: Re: Help adding a static, compile time constant variable to a class/struct in C++
Post by: Hooman on June 30, 2017, 04:07:22 AM
It seems to, yes. Though I would see myself using quotes anyway. Just doesn't feel right to me without them.

Similarly I always use parenthesis with sizeof operator, even though they're not required.
Code: [Select]
sizeof(int)
sizeof int
Title: Re: Help adding a static, compile time constant variable to a class/struct in C++
Post by: Vagabond on July 01, 2017, 01:03:06 PM
Okay, thanks.

I'll review and ensure there are quotes around #error message within XFile.

-Brett