In C++11, infinity()
is constexpr
, so in theory you could use it directly this way:
char Buffer_format_text[std::numeric_limits<const int>::infinity()];
However, the problem here is that int
cannot represent infinity. If you tried this:
std::cout << std::numeric_limits<const int>::has_infinity;
You would see that 0
(false
) is printed to the standard output (live example). The infinity()
function for specializations of std::numeric_limits
where has_infinity
is false
will return 0 - in fact, the function is meaningless in those cases - and you cannot create arrays of size 0.
Besides, you cannot expect an array of infinite size to be allocated - how would it fit into memory? The right approach, if you do not know in advance the size of your vector, is to use std::vector
or a similar container that does allocate memory upon request.
UPDATE:
It seems what you actually need an infinite array for is to be able to build up a string whose size is not known in advance. To encapsulate such a string that grows dynamically, you can use std::string
.
In order to perform type-safe output into an std::string
and replace sprintf()
, you could use std::ostringstream
. That would allow you to insert stuff into a string the same way you would print it to the standard output.
Then, once you are done working with the std::ostringstream
, you can get an std::string
object from it by calling the str()
member function.
This is how you could use it in a simple example:
#include <string>
#include <sstream>
#include <iostream>
#include <iomanip>
int main()
{
std::ostringstream oss;
oss << "The answer is: ";
oss << 42;
oss << ", and this is a double: ";
oss << 3.14;
oss << ". " << std::endl;
oss << "Oh, btw, you can also output booleans: ";
oss << std::boolalpha << true;
oss << ". See? It's easy!" << std::endl;
std::cout << oss.str();
}
Live demo.