Skip to content

Commit f20a408

Browse files
committed
cpp/template.cpp: restart from scratch, mess too great to fix
cpp: constexpr if
1 parent f71b6e3 commit f20a408

5 files changed

Lines changed: 73 additions & 922 deletions

File tree

‎cpp/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
1. [Virtual inheritance](http://stackoverflow.com/questions/21558/in-c-what-is-a-virtual-base-class)
5959
1. [namespace](namespace.cpp)
6060
1. [template](template.cpp)
61+
1. [constexpr if](constexpr_if.cpp)
6162
1. [exception](exception.cpp)
6263
1. [typedef](typedef.cpp)
6364
1. [typecast](typecast.cpp)

‎cpp/constexpr_if.cpp‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// https://stackoverflow.com/questions/12160765/if-else-at-compile-time-in-c/54647315#54647315
2+
// https://stackoverflow.com/questions/37617677/implementing-a-compile-time-static-if-logic-for-different-string-types-in-a-co
3+
4+
#if __cplusplus >= 201703L
5+
#include <cassert>
6+
#include <type_traits>
7+
8+
template <class T>
9+
struct MyClass {
10+
int myFunc() {
11+
if constexpr(std::is_integral<T>())
12+
return 1;
13+
else
14+
return 2;
15+
}
16+
};
17+
#endif
18+
19+
int main() {
20+
#if __cplusplus >= 201703L
21+
assert(MyClass<int>().myFunc() == 1);
22+
assert(MyClass<float>().myFunc() == 2);
23+
#endif
24+
}

‎cpp/preprocessor.cpp‎

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,43 @@
1-
/*
2-
# preprocessor
3-
4-
Almost the same as the C one:
5-
http://stackoverflow.com/questions/5085533/is-a-c-preprocessor-identical-to-a-c-preprocessor
6-
7-
The major differences are the predefined macros.
8-
*/
1+
// # preprocessor
2+
//
3+
// Almost the same as the C one:
4+
// http://stackoverflow.com/questions/5085533/is-a-c-preprocessor-identical-to-a-c-preprocessor
5+
//
6+
// The major differences are the predefined macros.
97

108
#include "common.hpp"
119

1210
int main() {
13-
/*
14-
# Standard preprocessor macros
15-
*/
11+
// # __cplusplus
12+
//
13+
// Defined only if using C++ compiler.
14+
//
15+
// Its value is the actual C++ version in use
16+
// in a similar way to __STDC_VERSION__
17+
//
18+
// C99 says that the C compiler must not define it.
19+
//
20+
// GCC 6.4 has the -std=c++17 option early, even before
21+
// __cplusplus == 201703L was reached.
1622
{
17-
/*
18-
# __cplusplus
19-
20-
Defined only if using C++ compiler.
21-
22-
Its value is the actual C++ version in use in a similar way to __STDC_VERSION__
23-
24-
Values:
25-
26-
- C++98: 199711L
27-
- C++11: 201103L
28-
29-
C99 says that the C compiler must not define it.
30-
*/
31-
{
32-
std::cout << "__cplusplus = " << __cplusplus << std::endl;
33-
34-
#if __cplusplus >= 201103L
35-
std::cout << "C++11 code" << std::endl;
23+
std::cout << "__cplusplus = " << __cplusplus << std::endl;
24+
#if __cplusplus == 199711L
25+
std::cout << "C++98" << std::endl;
26+
#endif
27+
#if __cplusplus == 201103L
28+
std::cout << "C++11" << std::endl;
29+
#endif
30+
#if __cplusplus == 201402L
31+
std::cout << "C++14" << std::endl;
3632
#endif
37-
}
33+
#if __cplusplus == 201703L
34+
std::cout << "C++17" << std::endl;
35+
#endif
36+
}
3837

3938
#ifdef __STDCPP_THREADS__
40-
// TODO why not defined even though I do have multithreading?
41-
std::cout << "__STDCPP_THREADS__" << __cplusplus << std::endl;
42-
assert(__STDCPP_THREADS__ == 1);
39+
// TODO why not defined even though I do have multithreading?
40+
std::cout << "__STDCPP_THREADS__" << __cplusplus << std::endl;
41+
assert(__STDCPP_THREADS__ == 1);
4342
#endif
44-
}
4543
}

‎cpp/regex.cpp‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
/*
2-
# regex
3-
4-
Finally they are supported!
5-
6-
Many types are supported: Javascript, grep, awk, ...
7-
8-
It is probably saner and more powerful to stick to Javascript regexes.
9-
*/
1+
// # regex
2+
//
3+
// Finally they are supported!
4+
//
5+
// Many flavours supported: JavaScript, grep, awk, ...
6+
//
7+
// It is probably saner and more powerful to stick to JavaScript regexes.
108

119
#include "common.hpp"
1210

0 commit comments

Comments
 (0)