First the code:
#pragma once
#include <type_traits>
// assign unique identifiers to types
template<typename ...>
class Family
{
static std::size_t identifier() noexcept
{
static std::size_t value = 0;
return value++;
}
template<typename ...>
static std::size_t family() noexcept
{
static const std::size_t value = identifier();
return value;
}
public:
using family_type = std::size_t;
template<typename ... Type>
static family_type type() noexcept
{
return family<std::decay_t<Type>...>();
}
};
// usage
using introspection_family = Family<struct IntrospectionRegistry>;
template<typename Structure>
void addIntrospectData(DataType introspection[MAX_TYPES], DataType const& dataType)
{
/* reserve index for this type in the introspection register */
const auto num = introspection_family::type<Structure>();
assert(num < MAX_TYPES);
introspection[num - 1] = dataType;
}
This code give an integer to each type and I use it in some kind of C++ introspection implementation.
The application isn't multi threaded.
When I compiled it in -O0, sometimes, the call to introspection_family::type<Structure>() blocks at __cxa_guard_acquire#plt and I get a deadlock.
When compiled with -O3, I don't have any problem, but that might just be because it becomes very difficult to reproduce.
__cxa_guard_acquire is used to ensure that the static variable is constructed before we access it but here it should be irrelevant as I'm not even in a threaded application.
Does anyone have any idea why this is happening ?
I use:
CXXFLAGS=-std=c++14 -O0 -g3 -pthread -Wall -Wextra -Werror
LDFLAGS= -g -pthread -lGLEW -lGLU -lGL -lSDL2_mixer -lSDL2
And I use gcc (Ubuntu 5.4.0-6ubuntu1~16.04.10) 5.4.0 20160609
Adding the compile option -fno-threadsafe-statics helped. Since your application is single thread, this is OK.
I had a similar issue. However, the reason in my code was recursion. __cxa_guard_acquire will block when called recursively. A proper fix was needed. I did not check your code if this could also be your problem.
Additional info: The lock used might have a different name than __cxa_guard_acquire. It might also work with recursion, but this is compiler/libc++ specific and you should not rely on that.
Related
After a compiler update from g++ v7.5 (Ubuntu 18.04) to v11.2 (Ubuntu 22.04), the following code triggers the maybe-uninitialized warning:
#include <cstdint>
void f(std::uint16_t v)
{
(void) v;
}
int main()
{
unsigned int pixel = 0;
std::uint16_t *f16p = reinterpret_cast<std::uint16_t*>(&pixel);
f(*f16p);
}
Compiling on Ubuntu 22.04, g++ v11.2 with -O3 -Wall -Werror.
The example code is a reduced form of a real use case, and its ugliness is not the issue here.
Since we have -Werror enabled, it leads to a build error, so I'm trying figure out how to deal with it right now. Is this an instance of this gcc bug, or is there an other explanation for the warning?
https://godbolt.org/z/baaMTxhae
You don't initialize an object of type std::uint16_t, so it is used uninitialized.
This error is suppressed by -fno-strict-aliasing, allowing pixel to be accessed through a uint16_t lvalue.
Note that -fstrict-aliasing is the default at -O2 or higher.
It could also be fixed with a may_alias pointer:
using aliasing_uint16_t = std::uint16_t [[gnu::may_alias]];
aliasing_uint16_t* f16p = reinterpret_cast<std::uint16_t*>(&pixel);
f(*f16p);
Or you can use std::start_lifetime_as:
static_assert(sizeof(int) >= sizeof(std::uint16_t) && alignof(int) >= alignof(std::uint16_t));
// (C++23, not implemented in g++11 or 12 yet)
auto *f16p = std::start_lifetime_as<std::uint16_t>(&pixel);
// (C++17)
auto *f16p = std::launder(reinterpret_cast<std::uint16_t*>(new (&pixel) char[sizeof(std::uint16_t)]));
In the following code, I create a Builder template, and provide a default implementation to return nothing. I then specialize the template with int, to return a value 37.
When I compile with -O0, the code prints 37, which is the expected result. But when I compile using -O3, the code prints 0.
The platform is Ubuntu 20.04, with GCC 9.3.0
Can anyone helps me understand the behavior?
builder.h
class Builder {
public:
template<typename C>
static C build() {
return 0;
}
};
builder.cc
#include "builder.h"
template<>
int Builder::build<int>() {
return 37;
}
main.cc
#include "builder.h"
#include <iostream>
int main() {
std::cout << Builder::build<int>() << '\n';
}
makefile
CXX_FLAG = -O0 -g
all:
g++ $(CXX_FLAG) builder.cc -c -o builder.o
g++ $(CXX_FLAG) main.cc builder.o -o main
clean:
rm *.o
rm main
You should add a forward declaration for build<int>() to builder.h, like so:
template<>
int Builder::build<int>();
Otherwise, while compiling main.cc, the compiler sees only the generic template, and is allowed to inline an instance of the generic build() function. If you add the forward declaration, the compiler knows you provided a specialization elsewhere, and will not inline it.
With -O3, the compiler tries to inline, with -O0 it will not inline anything, hence the difference.
Your code actually violates the "One Definition Rule": it will create two definitions for Builder::build<int>(), but they are not the same. The standard says the result is undefined, but no diagnostics are required. That is a bit unfortunate in this case, as it would have been helpful if a warning or error message was produced.
Here the simple code for compile-time repetition of a lambda. I compiled for AVR with -Os and -O2. With -Os the lambda isn't inlined but with -O2. The g++ manual says that -Os is same as -O2 but disables some optimizations which increase the code size. I wonder if I can tweak the g++ options to inline such simple lambdas.
#include <cstdint>
#include <utility>
volatile uint8_t x;
namespace detail {
template<auto... II, typename F>
void repeat_impl(std::index_sequence<II...>, F&& f) {
( ((void)II, f()) , ...);
}
}
template<auto N, typename F>
void repeat(F&& f) {
detail::repeat_impl(std::make_index_sequence<N>{}, static_cast<F&&>(f));
}
int main() {
repeat<10>([](){
x/=2;
});
}
These three seem to be related with inline, turned on at -O2 but not -Os:
-finline-small-functions
-findirect-inlining
-fpartial-inlining
You could add one by one to -Os. Although inline may happen in an early phase of compile & optimization, there might be possibility that a function becomes feasible for inline after another optimization preceding inline is applied.
I do not think always_inline attribute is in C++ standard. (not sure, though) If it is not, it depends on the compiler. Gcc seems to support the attribute. Adding the attribute to the function would result in the function inlined.
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>();
typeid(*ptr_foo).name();
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 -Werror):
error: expression with side effects will be evaluated
despite being used as an operand to 'typeid'
[-Werror,-Wpotentially-evaluated-expression]
typeid(*ptr_foo).name();
(Just a note: GCC does not claim that kind of potential problematic)
Question
Is there a way to get the information about the type pointed by a unique_ptr without generating that kind of warning?
Note: I am not talking about disabling -Wpotentially-evaluated-expression or avoiding -Werror.
Looks like following should work without warnings and give correct result for derived class
std::unique_ptr<Foo> ptr_foo = std::make_unique<Bar>();
if(ptr_foo.get()){
auto& r = *ptr_foo.get();
std::cout << typeid(r).name() << '\n';
}
I am reading a wav file,and pushing the data in a std::array in the end.
I need to make some operation on the chunks of data. So I thought this is a good opportunity to learn Eric Niebler's ranges.
I saw view_facade in manual page under "custom ranges" section but than I saw this question: link . Now I am not sure how to make a custom range class. Can anybody help me about that? The code below shows what I am trying to achieve.
#include <iostream>
#include <range/v3/all.hpp>
using namespace ranges;
using namespace std;
struct A
{
static constexpr size_t MAX_SIZE = 100000;
A ()
{
for ( size_t i = 0; i < MAX_SIZE; i++)
data[i] = i;
size = MAX_SIZE;
}
auto begin() const { return data.begin(); }
auto end() const { return data.end(); }
std::array< double , MAX_SIZE > data;
size_t size;
};
int main()
{
A instance;
RANGES_FOR(auto chunk, view::all(instance) | view::chunk(256)) {
}
return 0;
}
Part of compile output:
14:47:23: Running steps for project tryOuts...
14:47:23: Configuration unchanged, skipping qmake step.
14:47:23: Starting: "C:\Qt\Tools\mingw491_32\bin\mingw32-make.exe"
C:/Qt/Tools/mingw491_32/bin/mingw32-make -f Makefile.Debug
mingw32-make[1]: Entering directory 'C:/Users/Erdem/Documents/build-tryOuts-Desktop_Qt_5_4_2_MinGW_32bit-Debug'
g++ -c -pipe -fno-keep-inline-dllexport -std=gnu++1y -pthread -lpthread -O3 -g -frtti -Wall -Wextra -fexceptions -mthreads -DUNICODE -I"..\tryOuts" -I"." -I"..\..\..\..\range-v3-master\include" -I"D:\cvOutNoIPP\install\include" -I"..\..\..\..\Qt\5.4\mingw491_32\mkspecs\win32-g++" -o debug\main.o ..\tryOuts\main.cpp
In file included from ..\..\..\..\range-v3-master\include/range/v3/utility/iterator.hpp:28:0,
from ..\..\..\..\range-v3-master\include/range/v3/begin_end.hpp:24,
from ..\..\..\..\range-v3-master\include/range/v3/core.hpp:17,
from ..\..\..\..\range-v3-master\include/range/v3/all.hpp:17,
from ..\tryOuts\main.cpp:2:
..\..\..\..\range-v3-master\include/range/v3/utility/basic_iterator.hpp:445:22: error: 'constexpr const T& ranges::v3::basic_mixin<Cur>::get() const' cannot be overloaded
T const &get() const noexcept
^
------------ Update -------------------------------------------
If I add
CONFIG += c++14 the code almost compiles except auto return type deduction errors below :
main.cpp:22: deduced return type only available with -std=c++1y or -std=gnu++1y
to avoid those errors I am using CONFIG += c++1y.But in this case I am getting bunch of errors that I post in first place. I know from D language so called "Voldemort Types" are important, I don't want to give up return type deduction. Which flag in gcc should I use?
I'm still learning the range library myself, but my understanding is that things that expose STL-compatible begin() and end() methods can be used as a view. So for example, with your Reader class you could have
struct Reader {
// ...
auto begin() const { return rawData.begin(); }
auto end() const { return rawData.end(); }
};
You could then use view::all() to create a view around the Reader, something like
Reader r;
RANGES_FOR(auto chunk, view::all(r) | view::chunk(256)) {
...
}
As I say, I'm still learning the library myself, but hopefully this will help.