Category : constants

I’ve been trying the new modules feature, but I’m unable to export a global constant. The exporting seems to compile fine, but when importing the compiler complains that the constant is not declared. My code: test.cpp export module test; export struct my_type { int x, y; }; export constexpr int my_constant = 42; export int ..

Read more

Why doesn’t the compiler throw an error if I don’t initialize const variables in an unused template class? If I remove the template keyword the compiler complains as expected. This code works (unexpected): #include <iostream> template<class T> class Matrix { private: const uint8_t numRows, numColumns; T content[]; public: Matrix(uint8_t numRows, uint8_t numColumns) // : numRows(numRows), ..

Read more

I have a std::map of strings keyed by integers. I want the map to be readonly. const std::map<int, std::string> myMap = {values}; When I try and access a value via the subscript operator std::string value = myMap[intKey]; I get a compile error "E0349 no operator "[]" matches these operands." If I remove the const from ..

Read more

I have a class like this one: struct Example { std::unique_ptr<int> pointer_member; }; I’d like the type of pointer_member to be std::unique_ptr<int const> const when manipulating instances of Example const, and std::unique_ptr<int> when manipulating instances of Example. The best solution I could think of so far involves templates, but it’s really boilerplaty and not very ..

Read more

I am having issues with cin.getline(). When I put it into the below program, it is giving an error: no instance of overload function (ect) matches the argument list I am also having an issue with my variable nx. The compiler says that it cannot be a constant, but I’m not sure how it is. ..

Read more

#include <iostream> int main() { double val = -400.0; const double constVal = -400.0; std::cout << val << std::endl; std::cout << static_cast<unsigned>(val) << std::endl; std::cout << constVal << std::endl; std::cout << static_cast<unsigned>(constVal) << std::endl; return 0; } Output: -400 4294966896 -400 0 wtf mate? A moderate amount of Googling shows nothing on this matter. Source: ..

Read more