Created
June 7, 2021 09:30
-
-
Save jpret/00afde764ca5fe93c9f974f48c853e27 to your computer and use it in GitHub Desktop.
Constexpr string literals in anonymous namespace -> fix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "bar.h" | |
| #include <iostream> | |
| namespace bar { | |
| extern const char extern_delimiter[] = "</extern_delimiter>"; | |
| const char Foo::struct_delimiter[] = "</struct_delimiter>"; | |
| Bar::Bar() { | |
| std::cout << "Bar constructor!" << std::endl; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #pragma once | |
| #include <iostream> | |
| /* This class reads until the "Delimiter" */ | |
| template<const char *Delimiter> | |
| struct Reader { | |
| Reader() { | |
| std::cout << "Reader \"delimiter\" set to: " << Delimiter << std::endl; | |
| } | |
| }; | |
| namespace bar { | |
| // Option 1 | |
| extern const char extern_delimiter[]; | |
| // Option 2 - Gives "whose type uses the anonymous namespace" warning | |
| static constexpr char constexpr_delimiter[] = "</constexpr_delimiter>"; | |
| // Option 3 | |
| struct Foo { | |
| static const char struct_delimiter[]; | |
| }; | |
| struct Bar { | |
| Bar(); | |
| Reader<extern_delimiter> reader_extern_delimiter; | |
| Reader<constexpr_delimiter> reader_constexpr_delimiter; | |
| Reader<Foo::struct_delimiter> reader_struct_delimiter; | |
| }; | |
| }// namespace bar |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| cmake_minimum_required(VERSION 3.17) | |
| project(foo) | |
| set(CMAKE_CXX_STANDARD 11) | |
| add_executable(example main.cpp bar.cpp) | |
| /* Build output: | |
| [ 66%] Building CXX object CMakeFiles/example.dir/main.cpp.o | |
| [ 66%] Building CXX object CMakeFiles/example.dir/bar.cpp.o | |
| In file included from main.cpp:2:0: | |
| bar.h:27:8: warning: ‘bar::Bar’ has a field ‘bar::Bar::reader_constexpr_delimiter’ whose type uses the anonymous namespace [enabled by default] | |
| struct Bar { | |
| ^ | |
| In file included from bar.cpp:2:0: | |
| bar.h:27:8: warning: ‘bar::Bar’ has a field ‘bar::Bar::reader_constexpr_delimiter’ whose type uses the anonymous namespace [enabled by default] | |
| struct Bar { | |
| ^ | |
| [100%] Linking CXX executable example | |
| [100%] Built target example | |
| Build finished | |
| */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "bar.h" | |
| int main () { | |
| bar::Bar bar; | |
| return 0; | |
| } | |
| /* Console output: | |
| Reader "delimiter" set to: </extern_delimiter> | |
| Reader "delimiter" set to: </constexpr_delimiter> | |
| Reader "delimiter" set to: </struct_delimiter> | |
| Bar constructor! | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment