Am I using decltype wrong?

Hello. I am trying to write a program using gtkmm(C++ GTK+ bindings). The code compiles without errors or warnings but when I run it I get segmentation fault. My compiler is g++(GCC). I think it has to do with my use of decltype and assigning to that private member variable.

Here is the class definition and constructor:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Application 
{
public:
	Application(int argc, char **argv);
	virtual ~Application() {}
	
	int run();
	
private:
	decltype(Gtk::Application::create()) _app;
	MainWindow _appwin;
	
};


Application::Application(int argc, char **argv)
{
	_app = Gtk::Application::create(argc, argv);
}


All the code examples for gtkmm create the application like this:
auto app = Gtk::Application::create(argc, argv);

So what am I doing wrong here?
Last edited on
Well I changed the constructor and it fixed the problem:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Application 
{
public:
	Application(int argc, char **argv)
		: _app(Gtk::Application::create(argc, argv)) {}
		
	virtual ~Application() {}
	
	int run();
	
private:
	decltype(Gtk::Application::create()) _app;
	MainWindow _appwin;
	
};


Why does this work but putting it in the constructor body does not?
> The code compiles without errors or warnings but when I run it I get segmentation fault.
> My compiler is g++(GCC).

Ask g++ to compile the code as standard C++ ( -std=c++14 -pedantic-errors ); it does not do so by default.

Also turn on all warnings ( -Wall -Wextra )

1
2
3
4
5
6
// -g++ -std=c++14 -Wall -Wextra -pedantic-errors
// ***error: too few arguments to function 'int Gtk::Application::create(int, char**)'
// decltype(Gtk::Application::create()) _app;
// (assuming that there is no nullary overload of Gtk::Application::create)

decltype( Gtk::Application::create( 0, nullptr ) ) _app ; // should be fine 



> when I run it I get segmentation fault.
> I think it has to do with my use of decltype and assigning to that private member variable.

This particular Linuxism, though ugly, may not be the cause of the segfault. ** EDIT ** well
Compile with -std=c++14 -Wall -Wextra -pedantic-errors and see if there are other errors/warnings.


> Why does this work but putting it in the constructor body does not?

As far as C++ is concerned, in either case, it is an error; the program is ill-formed; an error diagnostic should have been issued.
Last edited on
Not sure what the error was because I re-compiled with the old code and now it doesn't segfault. I have -std=c++14 and -Wall already. I will add the others you mentioned for future.

Topic archived. No new replies allowed.