does an advanced c++ programmer know all of C++ features

Started by
73 comments, last by hplus0603 2 years, 9 months ago

Maybe there is a solution for a kind of static if i don't know about…

So i'd like to do something like this:

	template <class Vec3>
	void Stuff (const Vec3 &p)
	{
		Vec3 q;
		static_if (Vec3==SimdVec3)
		{
			q = SimdVec3::minPerElement(p, Vec3(1));
		}
		static_else
		{
			q[0] = min(p[0], 1);
			q[1] = min(p[1], 1);
			q[2] = min(p[2], 1);
		}

But seems not possible?

Advertisement

JoeJ said:

Maybe there is a solution for a kind of static if i don't know about…

So i'd like to do something like this:

	template <class Vec3>
	void Stuff (const Vec3 &p)
	{
		Vec3 q;
		static_if (Vec3==SimdVec3)
		{
			q = SimdVec3::minPerElement(p, Vec3(1));
		}
		static_else
		{
			q[0] = min(p[0], 1);
			q[1] = min(p[1], 1);
			q[2] = min(p[2], 1);
		}

But seems not possible?

Well there's “if constexpr ( ….) ”

Oh, and if you’re using pointers (other than function pointers), you’re doing it wrong.

JoeJ said:
// C:
for (size_t i=0; i<elements.size(); i++) elements[i].Do();

@joej Where does size() live, what is elements here in your “C” snippet?

JoeJ said:

Maybe there is a solution for a kind of static if i don't know about…

So i'd like to do something like this:

	template <class Vec3>
	void Stuff (const Vec3 &p)
	{
		Vec3 q;
		static_if (Vec3==SimdVec3)
		{
			q = SimdVec3::minPerElement(p, Vec3(1));
		}
		static_else
		{
			q[0] = min(p[0], 1);
			q[1] = min(p[1], 1);
			q[2] = min(p[2], 1);
		}

But seems not possible?

@joej Couldn't you specialize Stuff to run with <SimdVec3> that wouldn't just be static, it would be compile-time. Perhaps that doesn't solve your problem?

h8CplusplusGuru said:

if you want to create a named scope, you would use a lambda:

auto a = [&]()->auto{ int a = 10; return a+10; };
int b = a();

lambdas are the new c++ paradigm, you should practice them.

@h8cplusplusguru I think [&]()→auto is a little out there on account of the return type - at least I'd just infer the type if it's like that, but I get what you mean.

I tend to agree with @gnollrunner , it's most important to learn and practice general software development disciplines; algorithms, system architecture, project organisation.
The language features can be the sugar on the top, but sometimes they're so empowering that they change the landscape of the disciplines drastically. Like with templates and lambdas for instance, powerful stuff.
I enjoy a lot of things about C++. For instance that you can do something like metafunctions. That's just so cool, and isn't something I thought of first off, when I was introduced to templates.

Gnollrunner said:
Well there's “if constexpr ( ….) ”

Tried that, but you can not use it to branch depending on the type:

if constexpr (Vec3 == Vectormath::Aos::Vector3) // syntax error

SuperVGA said:
Where does size() live, what is elements here in your “C” snippet?

elements would be a std::vector<MyClass>, and the class has some Do() function.

SuperVGA said:
Couldn't you specialize Stuff to run with that wouldn't just be static, it would be compile-time. Perhaps that doesn't solve your problem?

Not really. I don't want to modify or unify all the various math libs i should not use but still do.
But i'd like to use certain functionality which only some but not all of them have, or the syntax differs between them. And the branch should be resolved in compile time.

Seems the only option would be specializations per type, but this defies using templates at all.
I see my wish is a bit fishy, but compilers should easily handle some static switch on type i guess.

JoeJ said:
Tried that, but you can not use it to branch depending on the type: if constexpr (Vec3 == Vectormath::Aos::Vector3) // syntax error

You absolutely can.

if constexpr (std::is_same_v<Vec3, Vectormath::Aos::Vector3>) // no syntax error

Thats why I said knowing those juicy c++ features is important ?

Tried that, but you can not use it to branch depending on the type:

There's always good old template specialization, but it's not so convenient.

Juliean said:
You absolutely can. if constexpr (std::is_same_v) // no syntax error Thats why I said knowing those juicy c++ features is important ?

Ha, thanks man!

Not even the guys at stackexchange knew about that juice :D

taby said:

Oh, and if you’re using pointers (other than function pointers), you’re doing it wrong.

What's wrong with using pointers?

I should have said that memory allocation using new and delete is generally a bad idea, because there are containers to do that job for you.

Otherwise, pointers are great.

This topic is closed to new replies.

Advertisement