Created
November 21, 2014 21:56
-
-
Save cppljevans/15d77d217e90e4a6c09a to your computer and use it in GitHub Desktop.
demo n3651 with macro's illustrating when "duplicate declaration" is needed (at least with clang3.5)
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
| //Purpose: | |
| // Test compiler implementation of | |
| // "variable templates" as described in: | |
| /* | |
| http://isocpp.org/files/papers/N3651.pdf | |
| */ | |
| // and as commented on here: | |
| /* | |
| http://en.cppreference.com/w/Talk:cpp/language/variable_template | |
| */ | |
| //Usage: | |
| // if defined(USE_DUP_DECL): | |
| // the "duplicate declaration" | |
| // (the term used at top of p. 3 of N3651) | |
| // outside the numeric_limits class template | |
| // *is* used. | |
| // else: | |
| // the "duplicate declaration" | |
| // outside the numeric_limits class template | |
| // *is not* used. | |
| // | |
| // if defined(USE_REF): | |
| // the reference of variable in the variable template | |
| // *is taken* | |
| // in the printT template function. | |
| // else: | |
| // the reference of variable in the variable template | |
| // *is not taken* | |
| // in the printT template function. | |
| //Result: | |
| // Using clang3.5 from: | |
| /* | |
| http://llvm.org/releases/3.5.0/clang+llvm-3.5.0-x86_64-linux-gnu-ubuntu-14.04.tar.xz | |
| */ | |
| // as it was on 2014-11-03: | |
| // if !defined(USE_REF): | |
| // compiles & runs OK (USE_DUP_DECL doesn't matter). | |
| // else if defined(USE_REF) && defined(USE_DUP_DECL): | |
| // compiles & runs OK | |
| // else (defined(USE_REF) && !defined(USE_DUP_DECL)): | |
| // fails link with error message: | |
| /* | |
| undefined reference to `numeric_limits<int>::min' | |
| */ | |
| //Conclusion: | |
| // when defined(USE_REF), *must* provide "duplicate declaration". | |
| //======================================= | |
| //#define USE_DUP_DECL | |
| template<typename T> | |
| struct numeric_limits | |
| { | |
| static const T min | |
| #if defined(USE_DUP_DECL) | |
| ; | |
| #else | |
| ={}; | |
| #endif | |
| }; | |
| #if defined(USE_DUP_DECL) | |
| //The following is the "duplicate declaration" | |
| //mentioned at the top of p. 3 of N3651. | |
| template<typename T> | |
| const T numeric_limits<T>::min = {};// definition of a static data member template | |
| #endif | |
| #include <iostream> | |
| template<typename T> | |
| void printT | |
| ( T | |
| #define USE_REF | |
| #ifdef USE_REF | |
| const& | |
| #endif | |
| a_t | |
| ) | |
| { | |
| std::cout<<a_t; | |
| } | |
| int main() | |
| { | |
| #if defined(USE_DUP_DECL) | |
| std::cout<<"defined(USE_DUPL_DECL)\n"; | |
| #else | |
| std::cout<<"!defined(USE_DUPL_DECL)\n"; | |
| #endif | |
| std::cout<<"numeric_limits<int>::min="; | |
| printT(numeric_limits<int>::min); | |
| std::cout<<"\n"; | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment