« first day (221 days earlier)      last day (2632 days later) » 

12:44 AM
Okay, C++ question, I need a small data structure to hold a variable number of integers, typically 4, sometimes 5 or 6. Some kind of flat_vector?
 
1 message moved from Lounge<C++>
@Mikhail Do you know the size at compile time, or not until run time? Once it's set does it remain constant, or can it change?
 
 
3 hours later…
3:33 AM
Could someone give me a hint for swapping the value of arbitrary bytes? So for the example I have 0x12345678, and I need to swap bytes 1 and 3, to get 0x56341278. I got as far as getting the values of 12000000 and 5600 through bitmasks, but I''m not sure of the general form of knowing whether to right or left shift to get them to the right place
 
1 message moved from Lounge<C++>
 
 
3 hours later…
6:18 AM
@ROODAY First off, in hexadecimal number each digit is called a nibble and not a byte because each hexadecimal digit is represented by just 4 bits.
@ROODAY Regarding the shifting, do the shifts literally as if you did the swap visually. Move/shift 0x12000000 to the right by 4*4=16 bits. Same for the 0x5600 but left shift.
 
 
3 hours later…
nwp
8:51 AM
@Mikhail use a vector implementation with small object optimization such as SmallVector
 
 
2 hours later…
10:41 AM
Hi guys
I was thinking about the question I made yesterday
Is there anyway I can achieve static/dynamic allocation using template metaprogramming instead?
the problem was the following I have a class where basically there's an array as data member
and I wish I could reuse most of the code
So basically I'd like to be able to switch from something like this
 
you can do what's called small vector optimization.
 
class MyClass {
 public:
  //methods and constructors
 private:
  int *mem;
};
to something like
class MyClass {
 public:
  //methods and constructors
 private:
  int mem[Fixed_Size];
};
what's that about?
 
essentially the type has a union{std::array<T, 16> arr; std::vector<T>} and a flag that specifies which is active
though putting types with destructors inside a union can be hairy
 
I don't think that's what I want
 
nwp
@user8469759 you can do that without templates, or pass Fixed_Size as a template parameter
 
10:45 AM
@nwp If I only pass Fixed_Size as a template parameter
the size has to be known
to compilation time
 
nwp
right
 
instead I'd like to have at least one chance
where I can change the size dinamically
dynamicallty
 
nwp
if you need to change the size dynamically then Fixed_Size is not the right thing for you, use a vector<int> for that instead
 
I want that fixed size as option as well
 
nwp
there are some hybrid things like what ratchet is talking about where you can store a fixed size of objects without dynamic memory allocation and if you add too much data it uses dynamic memory allocation, but that only helps you to squeeze out some performance you probably don't even need
 
10:48 AM
do you want your cake or do you want to eat it?
 
@ratchetfreak I'm not sure I understand
 
You can't have your cake and eat it (too) is a popular English idiomatic proverb or figure of speech. The proverb literally means "you cannot simultaneously retain your cake and eat it". Once the cake is eaten, it is gone. It can be used to say that one cannot or should not have or want more than one deserves or is reasonable, or that one cannot or should not try to have two incompatible things. The proverb's meaning is similar to the phrases "you can't have it both ways" and "you can't have the best of both worlds." Many people misunderstand the meanings of "have" and "eat" as used here but still...
 
nwp
you can do something like template <std::size_t Fixed_Size> class MyClass; and specialize for MyClass<0> that does dynamic memory allocation instead
 
@nwp that's probably what I'm looking for
however
there's a small issue
the difference between the static and the dynamic part
is that there are small code pieces where I do dynamic allocation, deallocation
nothing else
is there a way to make code instantiation in that way based on the template parameter?
or something similar
I know for sure you can make conditional function instantiation
 
nwp
@user8469759 yes, that is called template specialization. Though I doubt you will be very happy with it because it means you have to write the functions twice which was the problem you originally wanted to solve.
 
10:54 AM
May I ask why I need to write the functions twise?
twice* (sorry for the typo)
 
nwp
because you write it for MyClass<0> and MyClass<not 0>
maybe you can make one use the static functions from the other, but it will probably not be pretty
My suggestion is still to just use free functions and not care about visibility.
 
I have these functions it's something like
void my_op(uint32_t * const x, const int& par);
I can pass both the array of the form
int *
or
int v[10]
it doesn't make difference
I just want to write some wrapper classes
and the less they're for each case the better
anyway just to clarify these questions are more to clarify to myself what I can do and what I can't do
 
nwp
so the wrappers are basically the same as std::vector and std::array, except that they can call my_op
 
more or less these wrapper basically encapsulate a static array and a dynamic array
and if I for example
overload an operator
in one case
I'll do auxiliary preprocessing eventually to allocate the array
in the other case that's not necessary
however my_op would be called in both cases
I'm summarizing in this way... or at least I'm trying
What I would like to be able to do is somehow saying "ok, the data member is a dynamic one, so do this further processing and behave as usual"
but you can assume I have the helper functions
 
nwp
@user8469759 That is not a good thing to say. What you should want to say is "Do the thing, I don't care how". You don't want to have to deal with the dynamicness of data members if all you want to do is call my_op.
Or maybe you are talking about what the wrapper does internally, then it is the right thing to want to say.
 
11:19 AM
I'm talking about the wrapper
 
11:47 AM
is there any difference between "using" and "typedef"?
 
@Mikhail Never more than six?
Unbounded?
I couldn't find existing implementations at the time, so maybe there are some in boost or something these days. But they're simple enough.
 
nwp
@user8469759 using works with templates and is somewhat easier to read
 
 
2 hours later…
1:33 PM
is something like this correct
template<int L>
using my_type = std::conditional<L > 0, uint32_t[L],uint32_t*>::type
I mean it's not because my IDE says "syntax error"
should I use instead of uint32_t[L] the const pointer reference?
like here
124
Q: typedef fixed length array

341008I have to define a 24-bit data type.I am using char[3] to represent the type. Can I typedef char[3] to type24? I tried it in a code sample. I put typedef char[3] type24; in my header file. The compiler did not complain about it. But when I defined a function void foo(type24 val) {} in my C file, ...

there's the typedef
typedef char type24[3];
Any chance can be done the same using the "using" keyword and conditional?
 
add parenthesis to avoid the compiler seeing std::conditional<L > as a type?
 
I thought the white spaces worked fine
template<int L>
using MyType = std::conditional<(L > 0),uint32_t[L],uint32_t*>::type;
it doesn't work anyway
rror: need 'typename' before 'std::conditional<(L > 0), unsigned int, unsigned int*>::type' because 'std::conditional<(L > 0), unsigned int, unsigned int*>' is a dependent scope
I've tried with

template<int L>
using MyType = std::conditional<(L > 0),uint32_t,uint32_t*>::type;
 
nwp
2:01 PM
@user8469759 The error message says need 'typename' before 'std::conditional. Maybe you should listen to it.
it asks you to literally type the word typename there
 
why is that necessary?
doesn't std::conditional ... ::type already return a type?
 
the compiler tries to avoid solving the halting problem
and ::type is whatever you typed into the second and third template param, which doesn't need to be a type
 
what else could it be? given that the implementation is
template<bool B, class T, class F>
struct conditional { typedef T type; };
 
nwp
2:20 PM
@user8469759 there could be a specialization template<> struct conditional<true, int, int>{ void type();} and the compiler cannot know that
It is one of the dark corners of C++. If you value your sanity you just accept that if you have a type that has a :: in the type name you have to write typename.
 
are these things reported somewhere?
So when I think I'm getting crazy I could look up there
 
nwp
sure you can have a list of all the craziness of C++, but it will not help you not getting crazy
 
no, I mean
that specific bit
about the halting problem
that could occur
is it mentioned in standard draft?
 
nwp
Probably not. It is difficult enough to list all the rules without going into detail about why rules exist.
You could hunt down the original proposal about templates which might have a rational behind the decision.
 
ok
for the moment I'll stick at metaprogramming only
eventually for philosophical issue
I'll have a look
out of curiosity
for performance
Is metaprogramming good?
by performance I mean runtime, not compilation time
I assume compilation time can get much worse
but I wonder if metaprogramming can give good boost in performance
since most of the stuff is solved at compilation time
 
nwp
2:39 PM
the only general advice about performance you should listen to is "measure it"
 
what too would you recommend to measure performance?
I'm not aware of any actually
I know about profiling tools
but no one specifically
 
nwp
nonius.io maybe
 
there are 2 main ways to profile, instrumented and sampled
instrumented means you add code that grab the current time and logs is
sampled means that the profilier attaches as a debugger and regularly pauses the program and checks the callstack to see where time is spent
 
and what tools can be used both in the first and in the second case?
 
 
2 hours later…
5:06 PM
@user8469759 What do you want to learn from the measurements?
 
6:05 PM
@user8469759 Use KCachegrind is good for hotspot detection, but this approach typically can't be used for user facing GUI apps.
 
 
3 hours later…
8:46 PM
Hey! Can anyone help me with an error? I'm getting "no matching function for call to 'ReportError::IncompatibleOperands(Operator*&, Expr*&, Expr*&)'
note: candidates are: static void ReportError::IncompatibleOperands(Operator*, Type*, Type*)
". When I call the function, however, I'm passing in pointers which are class variables.
Anyone know why it would give that error?
 
2 messages moved from Lounge<C++>
SSCCE please
 

« first day (221 days earlier)      last day (2632 days later) »