C++ constexpr member pointers in classes - c++

I'm trying to use a class to group all the parameters of a template data structure, in particular an intrusive AVL tree. The user would do something like this:
struct MyEntry {
MyEntry *parent;
MyEntry *child[2];
int balance;
int value;
};
struct MyAvlTreeParams {
typedef MyEntry entry_type;
static constexpr auto parent_member = &MyEntry::parent;
static constexpr auto child_member = &MyEntry::child;
static constexpr auto balance_member = &MyEntry::balance;
... // comparators also come here which compare MyEntry::value
};
AvlTree<MyAvlTreeParams> tree;
MyEntry entry1, entry2;
entry1.value = 6;
entry2.value = 8;
tree.insert(&entry1);
tree.insert(&entry2);
But there is a problem with the member pointers in MyAvlTreeParams. This sample demonstrates it:
struct A {
int x;
};
struct B {
static constexpr auto member = &A::x;
};
int main ()
{
A a;
(a.*(B::member)) = 6;
return 0;
}
This works with clang++ 3.1, but g++ 4.7.2 fails to link with the error:
/tmp/ccGXGIOl.o:a.cpp:function main: error: undefined reference to 'B::member'
The error is fixed by adding the following declaration some place after the definition of struct B (see this question):
constexpr int (A::*(B::member));
To see how this becomes problematic in my case, all the following would need to be added whenever the AVL tree is used:
constexpr MyEntry * MyEntry::*(MyAvlTreeParams::parent_member);
constexpr MyEntry * (MyEntry::*(MyAvlTreeParams::child_member))[2];
constexpr int MyEntry::*(MyAvlTreeParams::balance_member);
Is there any way to do this without such information-less boilerplate code, or something different which achieves the same goals of grouping parameters (i.e. not just passing all the members as template parameters)?

Related

Initializing C struct with unions declared in C++17

This question is specifically about options in C++17. Assuming following declaration in C library (I cannot change them):
typedef enum {
TYPEA = 0,
TYPEB = 2,
TYPEC = 4
} SPECIFIC_TYPE_t;
typedef struct {
uint16_t Method : 1;
uint16_t Access : 2;
uint16_t VendorSpecific : 1;
uint16_t Direction : 1;
uint16_t Persistent : 1;
uint16_t Internal : 4;
uint16_t Reserved : 6;
} PROPERTY_t;
typedef RESULT_t (*CB_DataPointRead_t) (void *Service, uint8_t Pinpoint, bool VendorSpecific,
uint16_t GroupID, uint16_t ElementID, void *Data,
uint8_t *DataLengthInOut);
typedef RESULT_t (*CB_DataPointWrite_t) (void *service, uint8_t Pinpoint, bool VendorSpecific,
uint16_t GroupID, uint16_t ElementID, void *Data,
uint8_t DataLength);
typedef struct {
uint16_t GroupID;
uint16_t ElementID;
uint8_t Pinpoint;
SPECIFIC_TYPE_t Type;
uint8_t Size;
PROPERTY_t Property;
union {
struct {
CB_DataPointRead_t Read;
CB_DataPointWrite_t Write;
} Callback;
struct {
void *Data;
} DirectAccess;
} AccessType;
} DataPoint_t;
Status in pure C
To initialize the last element designator initializers work well in C:
uint16t dataPointValue = 0;
const DataPoint_t firstDatapointConfig = {
/*...*/,
.DirectAccess = { (void*)&dataPointValue; }
};
Designator initializers in C++
They appear in C++20 and aren't compatible with C in many aspects.
Problem
I'd like to initialize variable like firstDatapointConfig as const qualified in C++17. So far I don't see a way other than write a function in C (compiled as C code) and return the initialized structure to a variable before use. I tried various ways, including gnuc++17 which handles designator initializers, except it tells me:
error: 'const DataPoint_t' has no non-static data member named 'DirectAccess'
and MSVC don't digest this method of initialization at all without C++20 enabled.
Addressing the last element outside initializer, don't work either:
datapoint.DirectAccess = { &value };
results in the following error:
error: 'struct DataPoint_t' has no member named 'DirectAccess'
Comment
It was much easier to use these structures in Rust after processing them through bindgen :-)
Question
Is there a way to initialize the variable of DataPoint_t type in C++17 with DirectAccess element filled with the right value?
Is there a way to initialize the variable of DataPoint_t type in C++17 with DirectAccess element filled with the right value?
Yes, even though my way of doing it is a little cumbersome. There may be easier ways:
// Make a usable type of that anonymous entity:
using AccessType_t = decltype(DataPoint_t::AccessType);
// Use the new type and prepare what you need:
AccessType_t at;
at.DirectAccess.Data = nullptr;
// initialize your DataPoint_t
const DataPoint_t firstDatapointConfig{1,2,3,TYPEA,4, PROPERTY_t{}, at};
If you do this a lot you could make a helper function:
using AccessType_t = decltype(DataPoint_t::AccessType);
using Callback_t = decltype(AccessType_t::Callback);
using DirectAccess_t = decltype(AccessType_t::DirectAccess);
template<class U>
constexpr auto init_AccessType(U u) {
AccessType_t at;
if constexpr (std::is_same_v<U,Callback_t>) {
at.Callback = u;
} else if constexpr (std::is_same_v<U,DirectAccess_t>) {
at.DirectAccess = u;
} else {
// uninitialized
}
return at;
}
const DataPoint_t firstDatapointConfig{1,2,3,TYPEA,4, PROPERTY_t{},
init_AccessType(DirectAccess_t{nullptr})};
I'd like to initialize variable like firstDatapointConfig as const qualified in C++17
With all that C++ has to offer:
constexpr DataPoint_t create_DataPoint_t(uint16_t *v) {
DataPoint_t r{};
r.AccessType.DirectAccess.Data = v;
return r;
}
const DataPoint_t firstDatapointConfig = create_DataPoint_t(&dataPointValue);
with designated initializers:
const DataPoint_t firstDatapointConfig2 = {
.AccessType = {
.DirectAccess = {
.Data = &dataPointValue
}
}
};
Addressing the last element outside initializer, don't work either:
datapoint.DirectAccess = { &value };
Because there is no such element, there is datapoint.AccessType.DirectAccess.Data.
To initialize the last element designator initializers work well in C:
uint16t dataPointValue = 0;
const DataPoint_t firstDatapointConfig = {
/*...*/,
.DirectAccess = { (void*)&dataPointValue; }
};
The presented code is invalid - uint16t is meant to be uint16_t and ; is a typo. And still after fixing the typos, no, the presented code is invalid in "pure C" and "does not work well" godbolt link. There is no such thing as DirectAccess in DataPoint_t - there is such member in the unnamed union declared inside DataPoint_t. You can do in C:
const DataPoint_t firstDatapointConfig3_in_C = {
.AccessType = {
.DirectAccess = { (void*)&dataPointValue }
}
};
or
const DataPoint_t firstDatapointConfig4_in_C = {
.AccessType.DirectAccess = { (void*)&dataPointValue }
};
or
const DataPoint_t firstDatapointConfig4_in_C = {
.AccessType.DirectAccess.Data = (void*)&dataPointValue
};
The cast to void* is superfluous - all pointers are implicitly converted to void*. Note that the following:
const DataPoint_t firstDatapointConfig5_in_C = {
.AccessType = { (void*)&dataPointValue }
};
would be equal to:
const DataPoint_t firstDatapointConfig6_in_C = {
.AccessType.Callback.Read = (void*)&dataPointValue
};
Most probably you are coding under -fms-extensions with GNU gcc or with MSVC, in which case you should be aware that you are using an extension that imports unnamed structure members to parent structure. The code is invalid in "pure C", it's using an extension to C.

Initialization of static constexpr class member dependent on template argument type

It appears that I still do not understand C++'s constexpr well enough and come across weird aspects almost every day. Here I would like to ask about one specifically:
struct Part1 {
static constexpr int size = 10;
};
struct Part2 {
static constexpr int size = 24;
};
template<class ... Part>
struct Assembly {
// this does not compile ...
// static constexpr int sizeDirect = (... + Part::size);
// this works, Assembly<Part1, Part2>::sizeFromFct is 34
static constexpr int sumUp() { return (... + Part::size); }
static constexpr int sizeFromFct = sumUp();
};
I am testing with MSVC 2017 in conformant mode (/permissive-), and of course C++17.
Why does the first version not compile but the second one does? Can sizeDirect be made to work with some syntax tweak?

Undefined reference to static constexpr member only used by value

I was trying to create a clever class containing a font style. Before this consisted of 3 enums with bit-wise compatible values (each set of values did not had overlapping bits with the other enums) so you could do FontStyle::LEFT | FontStyle::TOP
But clang warned me about combining unrelated enums and yes I've seen possible bugs here: FontStyle::LEFT | FontStyle::RIGHT did set both bits. So I reworked the class using a helper class for the former enums and templates to match correct values. But now I'm getting linker errors for clang on Debug builds about undefined reference to my static constexpr members.
Looking at Undefined reference error for static constexpr member suggests, that the value is ODR-used, but I'm not using any references.
When does a static constexpr class member need an out-of-class definition? this then pointed me to the implicit copy constructor of my helper class which is the issue.
Is there any chance I can avoid the out-of-class definitions in C++14 (C++17 already allows to omit them) and Debug builds (Ctors are optimized away in Release and hence no undefined references)?
Related code:
#include <array>
#include <cstdint>
namespace detail {
template<unsigned T_index>
struct FontStylePart
{
constexpr FontStylePart(uint8_t val) : value(val) {}
uint8_t value;
};
} // namespace detail
class FontStyle
{
static constexpr unsigned AlignH = 0;
static constexpr unsigned AlignV = 1;
public:
constexpr FontStyle() = default;
template<unsigned T_index>
constexpr FontStyle(detail::FontStylePart<T_index> style) : FontStyle()
{
value[T_index] = style.value;
}
/// Horizontal align
static constexpr detail::FontStylePart<AlignH> LEFT = 0;
static constexpr detail::FontStylePart<AlignH> RIGHT = 1;
static constexpr detail::FontStylePart<AlignH> CENTER = 2;
/// Vertical align
static constexpr detail::FontStylePart<AlignV> TOP = 0;
static constexpr detail::FontStylePart<AlignV> BOTTOM = 1;
static constexpr detail::FontStylePart<AlignV> VCENTER = 2;
private:
std::array<uint8_t, 3> value = {{0, 0, 0}};
};
int main() {
FontStyle style = FontStyle::CENTER;
return 0;
}
The line
FontStyle style = FontStyle::CENTER;
is a ODR use of FontStyle::CENTER.
I tried using
constexpr FontStyle style = FontStyle::CENTER;
but I ran into problems in the constructor. The following works although it's not clear to me whether that is acceptable for your needs.
int main() {
constexpr auto v = FontStyle::CENTER;
FontStyle style = v;
return 0;
}
This transfers the onus of ODR use to constexpr auto v.

Unique Numerical ID for a Templated Class using Function Address

So, this question has been asked before, but I wanted a question with some of those key words in the title.
The issue is simple: How can I have a templated class, such that for each instance of the template - but not each instance of the class - there is a unique, numerical identifier?
That is, a way to differentiate:
foo<int> f1;
foo<char> f2;
classID(f1) != classID(f2);
but,
foo<int> f3;
foo<int> f4;
classID(f3) == classID(f4);
Related to:
in C++, how to use a singleton to ensure that each class has a unique integral ID?
Assigning Unique Numerical Identifiers to Instances of a Templated Class
template<class T>
class Base
{
public:
static void classID(){}
private:
T* t;
};
int main()
{
Base<int> foo;
Base<int> foo2;
Base<char> foo3;
/*
unsigned int i = reinterpret_cast<unsigned int>(Base<int>::classID);
unsigned int ii = reinterpret_cast<unsigned int>(Base<char>::classID);
unsigned int iii = reinterpret_cast<unsigned int>(Base<int>::classID);
/*/
unsigned int i = reinterpret_cast<unsigned int>(foo.classID);
unsigned int ii = reinterpret_cast<unsigned int>(foo2.classID);
unsigned int iii = reinterpret_cast<unsigned int>(foo3.classID);
//*/
return ((i != ii) + (i <= ii) + (i >= ii)) == 2;
}
That's how! It's lightweight, super easy, and doesn't use RTTI, although it uses the ridiculously unsafe reinterpret_cast.
Although, maybe I'm missing something?
I think you can just use a static function for this, and inherit its class:
struct IdCounter { static int counter; };
int IdCounter::counter;
template<typename Derived>
struct Id : IdCounter {
static int classId() {
static int id = counter++;
return id;
}
};
struct One : Id<One> { };
struct Two : Id<Two> { };
int main() { assert(One::classId() != Two::classId()); }
Of course, that won't be a static compile time constant - i don't think that is possible automatically (you would have to add those types manually to some type list like mpl::vector). Please notice that for just comparing types for equality, you don't need all this. You just need to use is_same (found in boost and other libraries and trivial to write) which yields a compile time constant
template<typename A, typename B>
struct is_same { static bool const value = false; };
template<typename A>
struct is_same<A, A> { static bool const value = true; };
int main() { char not_true[!is_same<One, Two>::value ? 1 : -1]; }

in-class initialization of non-integral static data

So I just learned via a compiler error that in-class initialization of arrays is invalid (why?). Now I would like to have some arrays initialized in a template class, and unfortunatly the contents depend on the template parameter. A condensed testcase looks like this:
template<typename T>
struct A {
T x;
static const int len = sizeof(T); // this is of course fine
static const int table[4] = { 0, len, 2*len, 3*len }; //this not
}
Any idea how to pull out the constant array?
EDIT: Added the 'int's.
Just as you'd do it without templates; put the initialization outside the class' declaration:
template<class T>
const int A<T>::table[4] = { 0, len, 2*len, 3*len };
class Y
{
const int c3 = 7; // error: not static
static int c4 = 7; // error: not const static const
float c5 = 7; // error not integral
};
So why do these inconvenient restrictions exist? A class is typically declared in a header file and a header file is typically included into many translation units. However, to avoid complicated linker rules, C++ requires that every object has a unique definition. That rule would be broken if C++ allowed in-class definition of entities that needed to be stored in memory as objects.
for more detail read : How do I define an in-class constant?
template <typename T, int index>
struct Table {
static const len = sizeof(T);
static const value = len*index;
};