As far as I known, std::string
is a non-POD type.
When I define a struct which contains std::string
field.
Can I still use the brace-init-list
to initialize the struct?
The code bellow works. Compiler gives me no warning. Am I missing something?
#include <stdio.h>
#include <string>
int main()
{
struct Book
{
int id;
std::string title;
};
Book book = {42, "hello, world"};
printf("%d:%s\n", book.id, book.title.c_str());
}
$ g++ --version
g++ (GCC) 4.1.2 20070925 (Red Hat 4.1.2-33)
$ g++ -Wall -std=c++98 main.cpp -lstdc++
$ ./a.out
42:hello, world