C++ floating point precision [duplicate]

To get the correct results, don’t set precision greater than available for this numeric type: #include <iostream> #include <limits> int main() { double a = 0.3; std::cout.precision(std::numeric_limits<double>::digits10); std::cout << a << std::endl; double b = 0; for (char i = 1; i <= 50; i++) { b = b + a; }; std::cout.precision(std::numeric_limits<double>::digits10); std::cout << … Read more

Overload resolution failure when streaming object via implicit conversion to string

14.8.1/4 in C++98 Implicit conversions (clause 4) will be performed on a function argument to convert it to the type of the corresponding function parameter if the parameter type contains no template-parameters that participate in template argument deduction. Here you would like an instantiation of template <class charT, class traits, class Allocator> basic_ostream<charT, traits>& operator<<(basic_ostream<charT, … Read more

Is it possible to emulate template?

After your update: no. There is no such functionality in C++. The closest is macros: #define AUTO_ARG(x) decltype(x), x f.bar<AUTO_ARG(5)>(); f.bar<AUTO_ARG(&Baz::bang)>(); Sounds like you want a generator: template <typename T> struct foo { foo(const T&) {} // do whatever }; template <typename T> foo<T> make_foo(const T& x) { return foo<T>(x); } Now instead of spelling … Read more

Convert string to int with bool/fail in C++

Use boost::lexical_cast. If the cast cannot be done, it will throw an exception. #include <boost/lexical_cast.hpp> #include <iostream> #include <string> int main(void) { std::string s; std::cin >> s; try { int i = boost::lexical_cast<int>(s); /* … */ } catch(…) { /* … */ } } Without boost: #include <iostream> #include <sstream> #include <string> int main(void) { … Read more

Why does printf() promote a float to a double?

Yes, float arguments to variadic function are promoted to double. The draft C99 standard section 6.5.2.2 Function calls says: […]and arguments that have type float are promoted to double. These are called the default argument promotions.[…] from the draft C++ standard section 5.2.2 Function call: […]a floating point type that is subject to the floating … Read more

std::cout won’t print

Make sure you flush the stream. This is required because the output streams are buffered and you have no guarantee over when the buffer will be flushed unless you manually flush it yourself. std::cout << “Hello” << std::endl; std::endl will output a newline and flush the stream. Alternatively, std::flush will just do the flush. Flushing … Read more

Isn’t the template argument (the signature) of std::function part of its type?

The problem is that both function<int()> and function<int(int)> are constructible from the same function. This is what the constructor declaration of std::function looks like in VS2010: template<class _Fx> function(_Fx _Func, typename _Not_integral<!_Is_integral<_Fx>::value, int>::_Type = 0); Ignoring the SFINAE part, it is constructible from pretty much anything. std::/boost::function employ a technique called type erasure, to allow … Read more