In addition, it's worth noting that "const" has certain limitations; compare:
[source lang="cpp"]struct C{ inline static int getval() {return 4;}};const int MAX=1024;const int MIN=C::getval();[/source]
"MAX" is a constant integral expression (can be used as an array-size in array declarations, as a case label in switch statements, etc.), while "MIN" is not.
See: http://www.devx.com/...tion/33327/1954
In C++11 there's a new declaration specifier, "constexpr", which allows you to solve this problem and, for example, do this:
[source lang="cpp"]constexpr int getDefaultArraySize (int multiplier){ return 10 * multiplier;}int my_array[ getDefaultArraySize( 3 ) ];// perfectly legal, "getDefaultArraySize( 3 )" is a constant integral expression equal to 30 at compile-time[/source]
See: http://www.cprogramm...-constexpr.html
More:
http://en.cppreferen...guage/constexpr
http://cpptruths.blo...texpr-meta.html
http://thenewcpp.wor...1/14/constexpr/
http://kaizer.se/wik...onstexpr_foldr/
Show differencesHistory of post edits
#4Matt-D
Posted 08 July 2012 - 02:44 PM
In addition, it's worth noting that "const" has certain limitations; compare:
[source lang="cpp"]struct C{ inline static int getval() {return 4;}};const int MAX=1024;const int MIN=C::getval();[/source]
"MAX" is a constant integral expression (can be used as an array-size in array declarations, as a case label in switch statements, etc.), while "MIN" is not.
See: http://www.devx.com/...tion/33327/1954
In C++11 there's a new declaration specifier, "constexpr", which allows you to solve this problem and, for example, do this:
[source lang="cpp"]constexpr int getDefaultArraySize (int multiplier){ return 10 * multiplier;}int my_array[ getDefaultArraySize( 3 ) ]; // perfectly legal, "getDefaultArraySize( 3 )" is a constant integral expression equal to 30 at compile-time[/source]
See: http://www.cprogramm...-constexpr.html
More:
http://en.cppreferen...guage/constexpr
http://cpptruths.blo...texpr-meta.html
http://thenewcpp.wor...1/14/constexpr/
http://kaizer.se/wik...onstexpr_foldr/
[source lang="cpp"]struct C{ inline static int getval() {return 4;}};const int MAX=1024;const int MIN=C::getval();[/source]
"MAX" is a constant integral expression (can be used as an array-size in array declarations, as a case label in switch statements, etc.), while "MIN" is not.
See: http://www.devx.com/...tion/33327/1954
In C++11 there's a new declaration specifier, "constexpr", which allows you to solve this problem and, for example, do this:
[source lang="cpp"]constexpr int getDefaultArraySize (int multiplier){ return 10 * multiplier;}int my_array[ getDefaultArraySize( 3 ) ]; // perfectly legal, "getDefaultArraySize( 3 )" is a constant integral expression equal to 30 at compile-time[/source]
See: http://www.cprogramm...-constexpr.html
More:
http://en.cppreferen...guage/constexpr
http://cpptruths.blo...texpr-meta.html
http://thenewcpp.wor...1/14/constexpr/
http://kaizer.se/wik...onstexpr_foldr/
#3Matt-D
Posted 08 July 2012 - 02:42 PM
In addition, it's worth noting that "const" has certain limitations; compare:
[source lang="cpp"]struct C{ inline static int getval() {return 4;}};const int MAX=1024;const int MIN=C::getval();[/source]
"MAX" is a constant integral expression (can be used as an array-size in array declarations, as a case label in switch statements, etc.), while "MIN" is not.
See: http://www.devx.com/...tion/33327/1954
In C++11 there's a new declaration specifier, "constexpr", which allows you to solve this problem and, for example, do this:
[source lang="cpp"]constexpr int getDefaultArraySize (int multiplier){ return 10 * multiplier;}int my_array[ getDefaultArraySize( 3 ) ];[/source]
See: http://www.cprogramm...-constexpr.html
More:
http://en.cppreferen...guage/constexpr
http://cpptruths.blo...texpr-meta.html
http://thenewcpp.wor...1/14/constexpr/
http://kaizer.se/wik...onstexpr_foldr/
[source lang="cpp"]struct C{ inline static int getval() {return 4;}};const int MAX=1024;const int MIN=C::getval();[/source]
"MAX" is a constant integral expression (can be used as an array-size in array declarations, as a case label in switch statements, etc.), while "MIN" is not.
See: http://www.devx.com/...tion/33327/1954
In C++11 there's a new declaration specifier, "constexpr", which allows you to solve this problem and, for example, do this:
[source lang="cpp"]constexpr int getDefaultArraySize (int multiplier){ return 10 * multiplier;}int my_array[ getDefaultArraySize( 3 ) ];[/source]
See: http://www.cprogramm...-constexpr.html
More:
http://en.cppreferen...guage/constexpr
http://cpptruths.blo...texpr-meta.html
http://thenewcpp.wor...1/14/constexpr/
http://kaizer.se/wik...onstexpr_foldr/
#2Matt-D
Posted 08 July 2012 - 02:41 PM
In addition, it's worth noting that "const" has certain limitations; compare:
[source lang="cpp"]struct C{ inline static int getval() {return 4;}};const int MAX=1024;const int MIN=C::getval();[/source]
"MAX" is a constant integral expression (can be used in as size in array declarations, as switch statement case labels, etc.), while "MIN" is not.
See: http://www.devx.com/...tion/33327/1954
In C++11 there's a new declaration specifier, "constexpr", which allows you to solve this problem and, for example, do this:
[source lang="cpp"]constexpr int getDefaultArraySize (int multiplier){ return 10 * multiplier;}int my_array[ getDefaultArraySize( 3 ) ];[/source]
See: http://www.cprogramm...-constexpr.html
More:
http://en.cppreferen...guage/constexpr
http://cpptruths.blo...texpr-meta.html
http://thenewcpp.wor...1/14/constexpr/
http://kaizer.se/wik...onstexpr_foldr/
[source lang="cpp"]struct C{ inline static int getval() {return 4;}};const int MAX=1024;const int MIN=C::getval();[/source]
"MAX" is a constant integral expression (can be used in as size in array declarations, as switch statement case labels, etc.), while "MIN" is not.
See: http://www.devx.com/...tion/33327/1954
In C++11 there's a new declaration specifier, "constexpr", which allows you to solve this problem and, for example, do this:
[source lang="cpp"]constexpr int getDefaultArraySize (int multiplier){ return 10 * multiplier;}int my_array[ getDefaultArraySize( 3 ) ];[/source]
See: http://www.cprogramm...-constexpr.html
More:
http://en.cppreferen...guage/constexpr
http://cpptruths.blo...texpr-meta.html
http://thenewcpp.wor...1/14/constexpr/
http://kaizer.se/wik...onstexpr_foldr/
#1Matt-D
Posted 08 July 2012 - 02:40 PM
As an addition, it's worth noting that "const" has certain limitations; compare:
struct C
{
inline static int getval() {return 4;}
};
const int MAX=1024;
const int MIN=C::getval();
MAX is a constant integral expression (can be used in as size in array declarations, as switch statement case labels, etc.), while MIN is not.
See: http://www.devx.com/cplus/10MinuteSolution/33327/1954
In C++11 there's a new declaration specifier, "constexpr", which allows you to solve this problem and, for example, do this:
constexpr int getDefaultArraySize (int multiplier)
{
return 10 * multiplier;
}
int my_array[ getDefaultArraySize( 3 ) ];
See: http://www.cprogramming.com/c++11/c++11-compile-time-processing-with-constexpr.html
More:
http://en.cppreference.com/w/cpp/language/constexpr
http://cpptruths.blogspot.com/2011/07/want-speed-use-constexpr-meta.html
http://thenewcpp.wordpress.com/2011/11/14/constexpr/
http://kaizer.se/wiki/log/post/C++_constexpr_foldr/
struct C
{
inline static int getval() {return 4;}
};
const int MAX=1024;
const int MIN=C::getval();
MAX is a constant integral expression (can be used in as size in array declarations, as switch statement case labels, etc.), while MIN is not.
See: http://www.devx.com/cplus/10MinuteSolution/33327/1954
In C++11 there's a new declaration specifier, "constexpr", which allows you to solve this problem and, for example, do this:
constexpr int getDefaultArraySize (int multiplier)
{
return 10 * multiplier;
}
int my_array[ getDefaultArraySize( 3 ) ];
See: http://www.cprogramming.com/c++11/c++11-compile-time-processing-with-constexpr.html
More:
http://en.cppreference.com/w/cpp/language/constexpr
http://cpptruths.blogspot.com/2011/07/want-speed-use-constexpr-meta.html
http://thenewcpp.wordpress.com/2011/11/14/constexpr/
http://kaizer.se/wiki/log/post/C++_constexpr_foldr/