Biagio Festa Biagio Festa - 3 years ago 253
C++ Question

Clang Warning on expression side effects

Given the following source code:

#include <memory>
#include <typeinfo>

struct Base {
virtual ~Base();

struct Derived : Base { };

int main() {
std::unique_ptr<Base> ptr_foo = std::make_unique<Derived>();


return 0;

and compiled it with:

clang++ -std=c++14 -Wall -Wextra -Werror -Wpedantic -g -o test test.cpp

Enviroment setup:

linux x86_64
clang version 5.0.0

It does not compile because of warning (note

error: expression with side effects will be evaluated
despite being used as an operand to 'typeid'

(Just a note: GCC does not claim that kind of potential problematic)


Is there a way to get the information about the type pointed by a
without generating that kind of warning?

Note: I am not talking about disabling
or avoiding

Answer Source

Looks like following should work without warnings and give correct result for derived class

std::unique_ptr<Foo> ptr_foo = std::make_unique<Bar>();

    auto& r = *ptr_foo.get();
    std::cout << typeid(r).name() << '\n';
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download