diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/chapter0/lesson13/standard.cpp | 43 | ||||
| -rw-r--r-- | src/chapter0/lesson7/helloworld.cpp | 7 | ||||
| -rw-r--r-- | src/chapter1/lesson4/unused_variables.cpp | 15 |
3 files changed, 65 insertions, 0 deletions
diff --git a/src/chapter0/lesson13/standard.cpp b/src/chapter0/lesson13/standard.cpp new file mode 100644 index 0000000..e27bc87 --- /dev/null +++ b/src/chapter0/lesson13/standard.cpp @@ -0,0 +1,43 @@ +// This program prints the C++ language standard your compiler is currently +// using + +#include <iostream> + +const int numStandards = 7; + +const long stdCode[numStandards] = {199711L, 201103L, 201402L, 201703L, + 202002L, 202302L, 202612L}; +const char *stdName[numStandards] = {"Pre-C++11", "C++11", "C++14", "C++17", + "C++20", "C++23", "C++26"}; + +long getCPPStandard() { +#if defined(_MSVC_LANG) + return _MSVC_LANG; +#elif defined(_MSC_VER) + return -1; +#else + return __cplusplus; +#endif +} + +int main() { + long standard = getCPPStandard(); + + if (standard == -1) { + std::cout << "Error: Unable to determine your language standard.\n"; + return 0; + } + + for (int i = 0; i < numStandards; ++i) { + // If the reported version is one of the finalized standard + // codes then we know exactly what version the compiler is + // running + if (standard == stdCode[i]) { + std::cout << "Your compiler is using " << stdName[i] + << " (language standard code " << standard << "L)\n"; + break; + } + } + + return 0; +} diff --git a/src/chapter0/lesson7/helloworld.cpp b/src/chapter0/lesson7/helloworld.cpp new file mode 100644 index 0000000..b0a6bd2 --- /dev/null +++ b/src/chapter0/lesson7/helloworld.cpp @@ -0,0 +1,7 @@ +#include <cstdlib> +#include <iostream> + +int main() { + std::cout << "Hello, world!\n"; + return(EXIT_SUCCESS); +} diff --git a/src/chapter1/lesson4/unused_variables.cpp b/src/chapter1/lesson4/unused_variables.cpp new file mode 100644 index 0000000..52d724f --- /dev/null +++ b/src/chapter1/lesson4/unused_variables.cpp @@ -0,0 +1,15 @@ +#include <iostream> + +int main() +{ + [[maybe_unused]] double pi { 3.14159 }; // Don't complain if pi is unused + [[maybe_unused]] double gravity { 9.8 }; // Don't complain if gravity is unused + [[maybe_unused]] double phi { 1.61803 }; // Don't complain if phi is unused + + std::cout << pi << '\n'; + std::cout << phi << '\n'; + + // The compiler will no longer warn about gravity not being used + + return 0; +} |
