How to declare constexpr c-string member? [duplicate] - c++

This question already has answers here:
Undefined reference to static variable c++
(3 answers)
Undefined reference to static constexpr char[]
(6 answers)
Closed 3 years ago.
How to declare constexpr c-string member ? Non-member constexpr c-string can be declared this way:
constexpr char id_[] = "aaa";
so I have though that I can declare it this way:
struct T
{
const static constexpr char id_[] = "aaa";
};
but I get undefined reference error.

Related

Use of const and & in functions C++ [duplicate]

This question already has answers here:
C++ Return value, reference, const reference
(5 answers)
What is a reference variable in C++?
(12 answers)
Closed 1 year ago.
I am trying to understand the useage of 'const' and '&' in the following function declaration. I know that the last 'const' means the function cannot change member variables in the class and that 'const std::string& message' means the variable passed to the function cannot be changed, but I don't understand the meaning of 'const Logger&'. What is the purpose of this first 'const' and why is there an '&' following 'Logger'? Is this function meant to return an address or a pointer?
const Logger& log(const std::string& message) const;
So const Logger& is the return type of log. const means you will not be able to edit the return value at all. The return type Logger& means you'll get a reference to a Logger and not a copy of it.

How to resolve ambiguous function selection in C++? [duplicate]

This question already has answers here:
Overload resolution between object, rvalue reference, const reference
(1 answer)
C++ function overloading resolution involving pass-by-value, reference and constant reference
(3 answers)
Function Overloading Based on Value vs. Const Reference
(6 answers)
Closed 2 years ago.
How could i select the overloaded function that i mean to call ?
Consider this code:
void foo (std::vector<int> const &variable);
void foo (std::vector<int> variable);
For example in above code i want to call void foo (std::vector<int>);, I tried :
void bar ()
{
std::vector<int> tmp;
foo(tmp);
foo(static_cast<std::vector<int>(tmp));
foo(boost::implicit_cast<std::vector<int>>(tmp));
foo((std::vector<int>)tmp);
foo(std::vector<int>(tmp));
}
But it's failed, How could i do that without changing the functions signature ?

LLVM C++ : compiler fail to identify primitive type of class member as const [duplicate]

This question already has answers here:
Static constant string (class member)
(11 answers)
Why can't I have a non-integral static const member in a class?
(5 answers)
Why must non-integral static data members initialized in the class be constexpr?
(3 answers)
Closed 2 years ago.
Consider the following class in which I define 2 static variables with constant value and initialisation upon declaration.
Both variable are primitive, but for the string initialisation I get the following error when compiling with LLVM (clang) Non-const static data member must be initialized out of line even though its type is preceded by const. In visual studio, the same code passed compilation.
perhaps anybody can explain this discrepancy ?
class my class
{
public:
// the variable in this line is recognised as non-const.
// Therefore, it doesn't allow initialisation.
static const wchar_t* bla = L"000111";
// this line compiles perfectly well.
static const int bla2 = 128;
}

Why is constexpr with std::forward_as_tuple not working? [duplicate]

This question already has answers here:
how to initialize a constexpr reference
(3 answers)
Constexpr Class taking const references not compiling
(1 answer)
constexpr begin of a std::array
(1 answer)
Closed 3 years ago.
Why is the following not compiling? This is somehow counter-intuitive (not to say constexpr concepts are confusing):
#include <tuple>
int main() {
constexpr const int a = 0;
static_assert(a == 0, "Wups");
constexpr auto t2 = std::forward_as_tuple(a, a);
}
LIVE
I assumed that a is a compile-time constant expression which is clearly the case as I can use it in static_assert.
Background: I wanted to continue with constexpr tuple of reference to feed to other constexpr functions to do some compile-time computations

why const makes sense? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How come a non-const reference cannot bind to a temporary object?
This program:
int fun()
{
return 1;
}
int main()
{
const int& var = fun();
return 0;
}
My question is why I must put a const befor the var definition? If not ,g++ will give me an error ,goes something like "invalid initialization of non-const reference of type ‘int&’ from a temporary of type ‘int’."
what is the 'const' for ?
In this situation you need const because reference initialization requires a variable with an address, not simply a value. Therefore the compiler must create an anonymous variable which you cannot access other than through the reference; the compiler does not want you to access the variable that you did not declare.
If you would declare the variable explicitly, const would be unnecessary:
int tmp = fun();
int &var(tmp);