Q:

Usage before initialization of const member, is this expected bahviour of gcc and clang?

Consider the following snippet. Class test has a const member a and a member function fun which returns a. An initialization list is used to to initialize a in the constructor. However in the initialization list a lambda is used to initialize a with the returned value of fun. This leads to different behaviors of clang and gcc at compile and runtime, depending on the optimization level. Below the snippet and the different outputs at compile and runtime are listed. Is this expected behavior of gcc and clang?
#include <iostream>

class test{
    public:
    const int a;

    test(): a([this](){return fun();}()) {}

    int fun()
    {
        return a;
    }
};

int main()
{
    auto t = test();
    std::cout << t.a << '\n';
    return 0;
}

Compiletime:
clang++-5.0 -std=c++17 -Wall -Wextra -Weverything

lambda_in_initializer_list.cpp:7:15: warning: lambda expressions are incompatible with C++98
      [-Wc++98-compat]
    test(): a([this](){return fun();}()) {}
              ^
warning: 'auto' type specifier is incompatible with C++98 [-Wc++98-compat]
lambda_in_initializer_list.cpp:17:5: warning: 'auto' type specifier is incompatible with C++98
      [-Wc++98-compat]
    auto t = test();
    ^~~~
3 warnings generated.

clang++-5.0 -std=c++17 -Wall -Wextra -Weverything -O1

lambda_in_initializer_list.cpp:7:15: warning: lambda expressions are incompatible with C++98
      [-Wc++98-compat]
    test(): a([this](){return fun();}()) {}
              ^
warning: 'auto' type specifier is incompatible with C++98 [-Wc++98-compat]
lambda_in_initializer_list.cpp:17:5: warning: 'auto' type specifier is incompatible with C++98
      [-Wc++98-compat]
    auto t = test();
    ^~~~

g++ -std=c++17 -Wall -Wextra -Wpedantic

No output

g++ -std=c++17 -Wall -Wextra -Wpedantic -O1

lambda_in_initializer_list.cpp: In function ‘int main()’:
lambda_in_initializer_list.cpp:18:20: warning: ‘t.test::a’ is used uninitialized in this function [-Wuninitialized]
     std::cout << t.a << '\n';
                  ~~^

Runtime:
clang++-5.0 -std=c++17 -Wall -Wextra -Weverything

0

clang++-5.0 -std=c++17 -Wall -Wextra -Weverything -O1

4196112

g++ -std=c++17 -Wall -Wextra -Wpedantic

Non deterministic output.

g++ -std=c++17 -Wall -Wextra -Wpedantic -O1

0

A:

I didn't quite understood a question, but it seems like you are actually asking 'why gcc didn't warn you until you turned up optimization'. 
This is a known thing. Detecting undefined behavior in complex cases requires quite a lot of efforts on compiler side, and often is only done when you are optimizing code (since compiler is doing a lot of work anyways). Just something to keep in mind when you are dealing with real life-compilers.

