Code Blocks 16.01: why these warnings?

why i get [-Wreorder] warnings?
"warning: 'property<bool>::setf' will be initialized after [-Wreorder]"
and i have much more. but i don't know why these warnings. can anyone advice me?
Can you show some relevant code? It sounds like you're inside a constructor, and your initialization list is initializing things in a different order than what they're defined at.

See: https://stackoverflow.com/questions/1242830/constructor-initialization-list-evaluation-order

Members of a class are initialized in the order that they are declared inside the class definition. If you don't do it in order, the program will still try to initialize earlier member variables before initializing the one you're actually trying to initialize. To prevent this weird behavior, always make sure your order of member declaration matches the order in the initialization list.

1
2
3
4
5
6
7
8
9
10
11
class A {
  A()
  : a(3), // 1st
    b(a)  // 2nd
  {}
  
  int a;  // 1st
  int b;  // 2nd
};
int main() {}
Last edited on
heres the class that makes the warning:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
template <typename T>
class property
{
public:
    T PropertyValue;
    std::function<T(void)> getf=nullptr;
    std::function<void(T)> setf=nullptr;
public:

    property(const T &value): PropertyValue(value), setf(nullptr),getf(nullptr)
    {
        getf=nullptr;
        setf=nullptr;
        PropertyValue=value;
    }

    property(const property &value)  :  PropertyValue(value.PropertyValue) , getf(value.getf), setf(value.setf)
    {
    }

    property(std::function<T(void)> GetFunction=nullptr,std::function<void(T)> SetFunction=nullptr):setf(SetFunction),getf(GetFunction),PropertyValue()
    {
        setf=SetFunction;
        getf=GetFunction;
    }

    property& operator=(const T &value)
    {
        PropertyValue=value;
        if (setf!=nullptr)
            setf(value);
        return *this;
    }

    property& operator=(const property &value)
    {
        PropertyValue = value.PropertyValue;
        if (setf!=nullptr)
            setf(PropertyValue);
        return *this;
    }

    operator T()
    {
        if (getf!=nullptr)
            return getf();
        else
            return PropertyValue;
    }

    ~property()
    {
        getf=nullptr;
        setf=nullptr;
    }
};

#define GetProperty(x) std::bind(&x, this)
#define SetProperty(x) std::bind(&x, this, std::placeholders::_1)
//Sample:
//property<PropertyType> PropertyName{GetProperty(GetFunction),SetProperty(SetFunction)}; 

the initialization is done on both constructors. so what it's the problem?
was a 'shot on dark', but i found the problem.
when i comment the initializate constructor list:
1
2
3
4
5
property(std::function<T(void)> GetFunction=nullptr,std::function<void(T)> SetFunction=nullptr) //:setf(SetFunction),getf(GetFunction),PropertyValue()
    {
        setf=SetFunction;
        getf=GetFunction;
    }

several programmers teach me for always use the initilizate constructor list. but why i get that warning, when i use them?
It is a warning regarding the order of initialization. On line 21 the order initialization is different from order of variables in the class.

By the way: line 12 to 14 and 23/24 don't make sense. You set the same value twice.
for be optional argument.
and these problem was, too, to do with my IDE that came with the GCC compiler. so i did the change and now no warnings.
thanks for all
Last edited on
Registered users can post here. Sign in or register to post.