Questions tagged [in-class-initialization]

55 questions
6
votes
2 answers

in-class initialization of atomic

Why in this example struct Foo { atomic x = 1; }; the compiler (gcc 4.8) is trying to use the atomic& operator=(const atomic&) which is deleted, (hence the example wont compile), while here struct Bar { Bar() { x = 1; } atomic
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
6
votes
2 answers

Unique pointer in-class initialization

Suppose I have a unique_ptr member object that I want to initialize in-class, see the code below. Why do I have to use uniform initialization (curly braces)? The second declaration spits an error, something like so.cpp:10:31: error: expected…
vsoftco
  • 55,410
  • 12
  • 139
  • 252
5
votes
3 answers

Order of In Class Initialization versus Constructor Initialization List

I want to initialize a bunch of members in-class to keep the source file cleaner. However, the objects take an argument I only receive via constructor and can initialize either in the constructor initialization list or in the constructor via…
5
votes
2 answers

In-class static member initialization

Given struct X {}; constexpr auto x = X{}; struct S { static constexpr auto& rx = x; }; gcc 4.8 says error: non-constant in-class initialization invalid for static member 'S::rx' static constexpr auto& rx = x; …
5
votes
1 answer

Forward declaration, unique_ptr and in-class initializer

I have read Is std::unique_ptr required to know the full definition of T? and Forward declaration with unique_ptr?, but my question is more specific. The following compiles: // Compile with $ g++ -std=c++11 -c #include class…
ricab
  • 2,697
  • 4
  • 23
  • 28
4
votes
1 answer

With C++, I get pointer with 0xcdcdcdcd when creating a class - what is happening?

----------------EDIT----------------------- I was grabbing the wrong value for comparison, the cdcdcdcd was coming from somewhere else. I still have my methods throwing exceptions before they are even reached, but my problem lies elsewhere, I wish…
Steven
  • 181
  • 1
  • 2
  • 11
4
votes
1 answer

Retrieve default value of in-class initialized member

Is there any way of directly retrieving the default value of a member, which has been defined using in-class initialization? For example: struct Test { int someValue = 5; }; int main(int argc,char *argv[]) { auto val =…
Silverlan
  • 2,783
  • 3
  • 31
  • 66
4
votes
2 answers

In class initialization and initializer list

I have recently discovered that you cant have at the same time in class initialization and initializer list. The following code fails : struct s { int i=0; }; int main() { s s1; //s1.i = 0 //s s2={42}; //fails return 0; } If I…
Davidbrcz
  • 2,335
  • 18
  • 27
3
votes
2 answers

In-class initialization of vector with size

struct Uct { std::vector vec{10}; }; The code above creates vector that contains single element with value 10. But I need to initialize the vector with size 10 instead. Just like this: std::vector vec(10); How can I do this with…
anton_rh
  • 8,226
  • 7
  • 45
  • 73
3
votes
0 answers

default initialization, value initialization and in-class-initialization of object not getting properly?

You might feel code is long and I write lot of things but believe me it is quite simple. I referred these two answers on stack-overflow for understanding this. post1 & post2 Code #include class A { public: int…
3
votes
1 answer

Class Loading vs Initialisation: Java static final variable

Example.java public class Example { static final int i = 10; static int j = 20; static { System.out.println("Example class loaded and initialized"); } } Use.java import java.util.Scanner; public class Use { public…
3
votes
1 answer

Compile error with gcc when in-class initializing unique_ptr of incomplete type to nullptr

I'm writing some code using pimpl idiom with unique_ptr. When I tried to use in-class initialization to set unique_ptr to nullptr by default, gcc gave a compile error, while clang and msvc both successfully compiled the code. And if I didn't use…
X. Sun
  • 315
  • 3
  • 15
3
votes
2 answers

In-class member initialization with an initializer list using uniform initialization syntax?

I am trying to compile the following with MSVC2013: class SomeClass { struct SomeStruct { bool a,b,c; }; SomeStruct ss{false, false, false}; } The compiler gives me the following error: SomeClass::SomeStruct::SomeStruct: no overloaded…
3
votes
3 answers

Why do I need another set of braces after brace-initializing a member variable?

I was trying to use brace-initialization (which thankfully Visual Studio 2013 actually supports), but for some reason when I do it on a class, it requires two sets of braces. For example: class NumberGrabber { int number; public: …
sircodesalot
  • 11,231
  • 8
  • 50
  • 83
2
votes
1 answer

What exactly is the in-class-initializer?

I've read many text mentioned the in-class-initializer and I've searched many question on stackoverflow, However I didn't found any precise explanation on what is the in-class-initializer. And as far as I understood the variable of build-in type…
SLN
  • 4,772
  • 2
  • 38
  • 79