« first day (373 days earlier)      last day (2704 days later) » 

04:17
im doing some Core Audio stuff in iOS and it requires a little bit of c++ in it. I was wondering the point of the "WaveSynthProc();" here is..

class WaveSynthProc : public DSPKernel
{
public:
enum {
InstrumentParamVolume = 0,
InstrumentParamWaveform = 1
};

WaveSynthProc();

void init(int channelCount, double inSampleRate);
void reset();
void setBuffers(AudioBufferList* outBufferList);

void startRamp(AUParameterAddress address, AUValue value, AUAudioFrameCount duration) override;
void handleMIDIEvent(AUMIDIEvent const& midiEvent) override;
not the meaning of the code, but syntactically what it means to apparently be calling the class as a function in the class body like that
1 message moved from Lounge<C++>
1 message moved from Lounge<C++>
05:04
That's just declaring a constructor for the class.
Ron
Ron
05:55
What happens if you cut out an array like in this question? What does the standard say when there are too many initializers?
Is it UB or are you guarantied to have 3 elements in an array when having a trivial code like this ` char c[3] = {'a', 'b', 'c', 'd'}`?. I would presume the compiler will discard the remainders.
Ron
Ron
06:10
Eer scratch that please, this won't even compile as a C++ program.
@Ron Ladies and gentlemen, we have a winner!
Ron
Ron
@JerryCoffin Embarrassingly enough I was reading the comments under my answer and totally overlooked that tiny bit.
@JerryCoffin In your experience, can you get away without knowing any C if working as a C++ developer?
@Ron I don't have any experience with that--I knew (and wrote) C for a few years before C++ compilers were easily available. But it certainly seems like it should be fine.
@Ron Yup, to be precise (§[dcl.init.string]/2): "There shall not be more initializers than there are array elements."
Ron
Ron
06:28
@JerryCoffin Appreciate.
 
1 hour later…
07:41
Hey, I'm using C++ json nlohman I'm trying to add vector<json> to an json item but I'm geting weird error..

itemData["child"] = getIDataItr(item);
Error :
Class 'vector<json>' is not compatible with class 'basic_string<char>' does any1 know what does this mean ?
 
5 hours later…
12:40
@Dariusz yes, this means one of the types is std::vector<json> and the other is std::string
 
2 hours later…
14:19
hi guys
silly question
I'm reading through "EffectiveC++"
specifically the point "use const whenever you can"
and there's an example that does make sense to me
it's something like
nwp
nwp
@user8469759 I hope you mean "Effective Modern C++"
(no sorry, the basic one, but I'm reading through that one as well)
anyway
it gives as example something like const Rational operator*(const Rational& lhs, const Rational& rhs);
and this prevents from typo like
a*b = c
which I agree, and I thought it was good
so I thought "ok, let me see if something like this is exploited in the stl library"
so I looked up the map container for example
and there's the operator[] which returns T&
and I was wondering if in such case would have made more sense declaring
const T& operator[]
is this nonsense?
nwp
nwp
The question is if map[key] = new_value; should be allowed or not. The STL thinks it should.
If you think such code makes no sense then the const T& version would be correct.
so that depends from the situation
but
nwp
nwp
@user8469759 "Effective C++" teaches you outdated C++ really well. Would not recommend.
14:25
I'm just looking up some points I think are interesting
would say that the operator* I mentioned is no sense then?
because compared to this one
I could have the problem a*b = c
nwp
nwp
@user8469759 That one is fine.
is that right?
nwp
nwp
The auto_ptr and some others are what you need to stay away from.
what do you mean?
nwp
nwp
"Effective C++" teaches you to use auto_ptr which is terrible and has been removed from C++.
14:29
so you're suggesting not to look upauto_ptr in the book I mentioned
is that right?
nwp
nwp
@user8469759 Honestly I wouldn't even care about that. I don't see anyone accidentally writing that and wishing that the compiler had caught it. It might actually still give you a warning for having no effect.
but I'm confused how is the auto_ptr related to my question?
nwp
nwp
@user8469759 It would make more sense to not look at that book at all. You can consider "Effective Modern C++" the same book with most bugs fixed.
@user8469759 It doesn't.
ok
and then
is the complex class in the stl affected by the problem I said?
or is there something subtle I'm missing
?
nwp
nwp
Try it out and see if complex1 * complex2 = complex3; compiles.
14:33
let me try but I assume it doesn't
it does
that's interesting
#include <iostream>
#include <complex>
int main()
{
    std::complex<double> c1(2, 0);
    std::complex<double> c2(0, 1);
    std::complex<double> c3(0.5,0.5);
    c3*c2 = c1;
    return 0;
}
now my question would be like
is that a feature left on purpose
because I don't see any use case of something like
c3*c2 = c1
it literally doesn't change anything xD
it's just useless
it's actually meaningless as expression I suppose
15:11
There's this code
Sorry let me indent it
nwp
nwp
you can edit your posts for 2 minutes
class CTextBlock {
    public:
        ...
        std::size_t length() const;
    private:
        char *pText;
        std::size_t textLength; // last calculated length of textblock
        bool lengthIsValid; // whether length is currently valid
};

std::size_t CTextBlock::length() const
{
    if (!lengthIsValid) {
        textLength = std::strlen(pText); // error! can’t assign to textLength
        lengthIsValid = true; // and lengthIsValid in a const
    } // member function
    return textLength;
basically
the method length() is for const objects, however still effective C++ says that textLength and lengthIsValid can be modified
why?
"This implementation of length is certainly not bitwise const — both textLength
and lengthIsValid may be modified — yet it seems as though it
should be valid for const CTextBlock objects. Compilers disagree. They
insist on bitwise constness"
sorry probably I'm not understanding that code snippet
nwp
nwp
The point is that the length function looks like it should be const, but it can't.
ok, and why is that?
nwp
nwp
Because getting the length of a text block shouldn't change the text block.
15:17
I don't understand
nwp
nwp
Given a text block, you wonder how many characters it contains. So you count them. And you expect that after counting you have the same text block as you started and not a different one because you counted the characters.
Therefore the counting function should be const because it doesn't change the text block.
ok, and isn't this what's happening in there?
nwp
nwp
@user8469759 No. It doesn't compile as you can see in the comments.
So you have to make length not-const in order to make the compiler happy. Which is ... wrong.
oh
yeah ok, it's just the implementation it's weird
I would've just counted the characters and returned the value, he's instead caching the length
but he cannot do that without violating the constness
nwp
nwp
@user8469759 Exactly. So he introduces mutable. Or he would, if that had existed when the book was written.
Hence not a good book.
15:23
what?
sorry sometimes I struggle to follow you
what sense does it make to declare phony clean target.?
in makefile
nwp
nwp
@user8469759 The solution to the problem of caching a value in a const function is to use the keyword mutable which didn't exist when Modern C++ was written. So the author advices you to do something wrong because it was the best at the time, but not anymore.
and what would be a better solution nowdays?
nwp
nwp
using mutable
but that's what he says later
he's giving an example of crap solution that doesn't work and later he says "mutable solves the problem"
nwp
nwp
15:30
Then you are using Effective Modern C++. mutable is a feature from C++11, so from about 2011-ish. Effective C++ is a book from 2005. Hence it cannot teach you about mutable.
i meant .PHONY: clean
@nwp this is what I'm using
Effective C++
Third Edition
55 Specific Ways to Improve Your Programs and Designs
the other one you mention is another book
@nwp Standard C++ has always included mutable.
nwp
nwp
oh, I looked at this and read the C++11. My bad.
@user8469759 that advice is outdated
15:34
why is that?
please elaborate
returning by const value is detrimental now
because it prohibits moving from it
wait wait
what are you talking about? the a*b = c or this discussion about caching the length and solved using mutable?
arrows, man
I'm purposefully using arrows to respond to a specific message
sorry
ok
you were saying?
15:38
@milleniumbug ?
sorry, I interrupted you. Please carry on
@user8469759 coliru.stacked-crooked.com/a/a7613ccde248c965 <- first returns by const, the latter doesn't. Note that this example has used -fno-elide-constructors. Without it, in this case both would be elided to just a default constructor and a destructor
user6377043
is it good practice to make a std::unique_ptr<std::array<unsigned char, 4096>> for a buffer?
what the hell is this? noisy(noisy&&)
?
That's a move constructor of the class noisy
don't know what is
15:44
1117
Q: What are move semantics?

dicroceI just finished listening to the Software Engineering radio podcast interview with Scott Meyers regarding C++0x. Most of the new features made sense to me, and I am actually excited about C++0x now, with the exception of one. I still don't get move semantics... What are they exactly?

@Advancid Roughly 99.99999% of the time, you just want std::array<char, size>. Why do you (think you) want a unique_ptr involved? The main point of std::array is to avoid dynamically allocating the space. If you're going to allocate the space dynamically anyway, chances are pretty good that you might as well just use vector.
@Advancid That's a pointer to an array of a size known at compile time. It's joins the disadvantages of std::vector and disadvantages of std::array
user6377043
I want to have an array on the heap but I though that maybe std::array would be more optimized than vector because it knows its size and it only has to allocate once
std::vector has to allocate multiple times only if you push_back without reserveing
if you set the size once, it allocates once
that said, bound checking could possibly be easier to check with a size known at compile time, but that's only relevant if you use .at
operator[] does no bound checking by default
@milleniumbug so the move constructor is basically a constructor called when a temporary rvalue is created
is that correct?
15:55
it's a constructor called when a rvalue is passed to it
(not just temporaries)
ah allright
and what did u mean by "movable"
?
nwp
nwp
16:31
Why does this not compile?
16:47
> Class template argument deduction is only performed if no template arguments are provided. If at least one argument is specified, deduction does not take place.
nwp
nwp
what about without <int>?
17:48
@nwp An initializer list has no type, so the compiler can't deduce much from it.
nwp
nwp
I found the implementation from the link, and auto a = std::array{1, 2, 3, 4}; works as advertised.
And std::array a{1, 2, 3, 4}; also compiles inside functions, but not inside structs.
combination of several garbage C++ features not working? not shocked
 
3 hours later…
20:30
I was trying a code to sort an array but it's cout is giving only "41"
#include<bits/stdc++.h>
using namespace std;
int main()
{
	int A[6]={31,41,59,26,41,58};;
int j;
int length = 6;
	for(j=2;j<length;j++)
	{
		int key;
		key = A[j];
		int i;
		i=j-1;
		while(i>0 && A[i]>key)
		{
			A[i+1]=A[i];
			i=i-1;
		}
	A[i+1]=key;

	}
cout<<A[j];

return 0;
}
any help?
#include <bits/stdc++.h> is garbage code
nwp
nwp
@Saubhagya std::sort(std::begin(A), std::end(A));
trying to do this with the above code new to algos what's wrong with the code ?
thanks for pointing it out i'll stick with #include <iostream> and <algorithm> @milleniumbug
86
Q: Why should I not #include <bits/stdc++.h>?

Lightness Races in OrbitI posted a question with my code whose only #include directive was the following: #include <bits/stdc++.h> My teacher told me to do this, but in the comments section I was informed that I shouldn't. Why?

^ if you want to know why exactly is it bad
yes i googled it :) can you please help me with the code above?
nwp
nwp
20:34
@Saubhagya If you want to make this work you should be able to run the example on a piece of paper and understand each step. Then write a function to print the array and probably j too and call it after every meaningful step. Then check where the output starts to differ from the paper version.
already tried it but i am not able to resolve it @nwp
nwp
nwp
@Saubhagya So, show the outputs that you expect on paper and the outputs of the program and the lines that don't do what you thought they would do.
You must at least say which sorting algorithm this is supposed to be.
bubble sort
i am feeding in 31,41,59,26,41,58 and expecting it to be out as a sorted array of increasing order
20:38
yes but you're writing out a single value...?
also out of bounds
nwp
nwp
@Saubhagya Here you have an example of all the steps bubblesort does along with pseudo code to check your application against.
index is exceeding the array's size?
@nwp i already referred to the online material ...
@milleniumbug i tried putting it in the for loop to print out the value as the j changes but it's giving out "59" 6 times
so your sorting doesn't work
what IDE are you using?
because you should start up a debugger and step in through the statements
i have tried few online, even coliru.stacked-crooked.com is giving out 59 x 6 times after changing the code to this
#include<bits/stdc++.h>
using namespace std;
int main()
{
	int A[6]={31,41,59,26,41,58};;
int j;
int length = 6;
	for(j=2;j<length;j++)
	{
		int key;
		key = A[j];
		int i;
		i=j-1;
		while(i>0 && A[i]>key)
		{
			A[i+1]=A[i];
			i=i-1;
		}
	A[i+1]=key;
cout<<A[j];

	}

return 0;
}
moved the cout up to check if that changes the cout but it's stuck
download an actual IDE, install it and start debugging then
20:46
@nwp imgur.com/a/Km7P7 << the pen paper way already tried :P
tried it on Dev C++ @milleniumbug still the same
nwp
nwp
@Saubhagya That one says insertion sort. I meant a list of numbers that get a little more sorted every step until the algorithm is done.
ok, now that you've downloaded and installed an IDE, start debugging
put a breakpoint before loop, put a watch on your array and then step in through the statements
A syntax error in expression, near `A[6]={31,4
that's not your code
that is, not the code you posted
->->error-begin
A syntax error in expression, near `A[6]={31,41,59,26,41,58}'.
21:23
the swap function was wrong
i tried to change it using a temp and it's still stuck with wrong output
nwp
nwp
I don't see a swap function in your code.
temp = A[i+1]; A[i+1]=A[i]; A[i]=temp
A[i+1]=A[i]; < this was wrong
but the output is still wrong
nwp
nwp
you should really use std::swap
i did make a program last day using <algorithm> but the thing is why this code isn't working?
39 mins ago, by milleniumbug
put a breakpoint before loop, put a watch on your array and then step in through the statements
21:28
@milleniumbug tried it
21:51
@milleniumbughttp://coliru.stacked-crooked.com/a/f26018db391654c8
suggestion ?
Hint: check where the array starts and where it ends
(that is, the indices)
tried changing the j=1
@milleniumbug now i am clueless why this isn't working...
okay it's working
while(i>=0 && A[i]>key) WOW
@milleniumbug thanks for the help.
22:53
Hello, i would like to know how this code snippet works
template<typename... T>
void printl(const T& ... t) {
(void)initializer_list<int>{ (cout << t, 0)... };
}
and what is the a generalization
nwp
nwp
It is a variadic template printing all the Ts. It has an ugly hack to work around the fact that (cout << t)...; is not legal C++, but that's essentially what it does.
no need to use initializer_list
I tend to use using swallow = int[]; (void)swallow{ (std::cout << t, 0)... };
the goal of swallow{ ... } is to introduce a context where pack-unpacking is legal and also, to introduce sequential evaluation
so here you have an int array filled with 0, while the return value of cout<<t is discarded?
essentially, yes
we ignore the array too
the important part is syntactic
is still valid code: { (f(t), g(t), 0)... } ?
23:04
yes
depending on how generic you want to be, you may want to protect yourself from crazy people overloading operator,
like this: (f(t), void(), g(t), void(), 0)
nwp
nwp
I vaguely remember STL saying that one void() at the end is enough.
yeah, possibly
I'm not exactly sure how this one works either
that is a cool feature even if at first it does not even look legal code
thanks much
does this feature have a name, in order to google it?
which one? void()?
the { (std::cout << t, 0)... } thing
23:12
this isn't a single feature, rather, several features used together in a specific manner, so, an idiom
I'm not sure if it has a specific name, but I call it a "swallow idiom"
this name doesn't seem to be popular outside of SO chat
yep, then the last question becomes: are there any other cases where you can use this idiom?
this one is variadic template specific (because of ...)
ok, fair enough
thanks

« first day (373 days earlier)      last day (2704 days later) »