# Error when returning a Pair<type,type> from a function

## Recommended Posts

Maybe today I'm confused and I'm missing the obvious...

I've made this simple struct inside my Utility.h

template<typename TFirst, typename TSecond>
struct Pair {
Pair(TFirst f, TSecond s):First{f},Second{s}{}
TFirst First;
TSecond Second;
};

And I'm using it from App.h (which #include "Utility.h") like so:

App.h

Pair<Uint32, Uint32> GetWindowSize();

App.cpp

Pair<Uint32, Uint32> App::GetWindowSize()
{
return Pair<Uint32, Uint32>(Width,Height);
}

And I'm getting a cascade of errors ALL on line 39 (image below).

Any idea what I am doing wrong this time? x_x

EDIT: please mod delete this topic, I just remembered Utility.h put everything inside namespace util, sorry x_x

Edited by MarcusAseth

##### Share on other sites

Why not use std::pair, which is significantly more full-featured and optimal?

Why are you using a pair to return a window size? A size is a conceptually richer concept, which includes a different set of operations and relationships than the more-generic "pair" concept. This seems like a prematurely-pessimized API design.

##### Share on other sites

I didn't wanted to have to add another include in all my files in order to use that, since I already had my Utility.h included I put my class inside of that. There is a way to add the std::pair to my Utility.h so that all files including it get to use std::pair without including anything else?

Quote

A size is a conceptually richer concept, which includes a different set of operations and relationships than the more-generic "pair" concept

I don't fully understand this, I basically just needed to pass around width and height so that I can spawn my Entities like so(code below:

EDIT: by the way, why the code below is being pasted in this crazy way?!  Maybe a problem in how the tab character is interpreted?

It should appear like this --> https://pastebin.com/iddWWWfH

GameMode::GameMode(SDL_Window* Window, SDL_Renderer* Renderer, App* App)
:Window{ Window }, Renderer{ Renderer }, AppRef{ App }, Running{ true }
{
float WindowWidth = static_cast<float>(AppRef->GetWindowSize().First);
float WindowHeight = static_cast<float>(AppRef->GetWindowSize().Second);

//PhysicsManager
PhysicsManager = std::make_unique<Physics>(this);

//Create Player
static_cast<int>(WindowWidth) / 2,
static_cast<int>(WindowHeight) - 40,
PivotMode::CENTER));

//Boundaries
//Right
Entities.push_back(std::make_unique<Boundary>(this,
Rect{ WindowWidth - 5.f, 0.f,
50.f, WindowHeight },
PivotMode::CENTER));
//Left
Entities.push_back(std::make_unique<Boundary>(this,
Rect{ -45.f, 0.f,
50.f, WindowHeight },
PivotMode::CENTER));
//Top
Entities.push_back(std::make_unique<Boundary>(this,
Rect{ 6.f, -45.f,
WindowWidth - 12.f, 50.f },
PivotMode::CENTER));

//Bottom
Entities.push_back(std::make_unique<Boundary>(this,
Rect{ 6.f, WindowHeight - 5.f,
WindowWidth - 12.f, 50.f },
PivotMode::CENTER));

//Create Ball
Entities.push_back(std::make_unique<Ball>(this,
static_cast<int>(WindowWidth) / 2,
200,
PivotMode::CENTER));
}

Edited by MarcusAseth

##### Share on other sites
11 minutes ago, MarcusAseth said:

EDIT: by the way, why the code below is being pasted in this crazy way?!  Maybe a problem in how the tab character is interpreted?

It is, a TAB character is defined to aligned at multiples of 8 characters, in ASCII. Most editors ignore this definition, and allow you to change it, giving the illusion that a TAB is whatever you configured. That of course fails as soon as your file leaves your editor, since other programs or other people use different TAB settings, or use the official size.

##### Share on other sites
2 minutes ago, Alberth said:

It is, a TAB character is defined to aligned at multiples of 8 characters, in ASCII. Most editors ignore this definition, and allow you to change it, giving the illusion that a TAB is whatever you configured. That of course fails as soon as your file leaves your editor, since other programs or other people use different TAB settings, or use the official size.

though this pastes correctly on codeshare.io and pastebin and Windows blocknote and pretty much everywhere else... wouldn't be better to have the site behaving the same way?

##### Share on other sites
Just now, MarcusAseth said:

though this pastes correctly on codeshare.io and pastebin and Windows blocknote

It can't paste correctly in all cases. TAB length isn't copied with the text, so either it guestimates, or it just happens to use the same length as you have. In both cases, you can break the formatting with a clever enough example.

24 minutes ago, MarcusAseth said:
Quote

A size is a conceptually richer concept, which includes a different set of operations and relationships than the more-generic "pair" concept

I don't fully understand this, I basically just needed to pass around width and height so that I can spawn my Entities like so(code below:

This means that if two numbers have a width and height relation, you can discuss other properties, eg "area". You can multiply 2 unrelated numbers, with each other, but without a width/height relation between them it has no useful meaning.

##### Share on other sites
58 minutes ago, MarcusAseth said:

I didn't wanted to have to add another include in all my files in order to use that, since I already had my Utility.h included I put my class inside of that. There is a way to add the std::pair to my Utility.h so that all files including it get to use std::pair without including anything else?

No, but including <utility> isn't going to be a huge deal, especially if you're just taking your Utility.h and turning into a convenience catch-all anyway. The net impact on compile time over the life of any nontrivial project will probably be smaller using <utility>, since it changes so infrequently, whereas your Utility.h will change quite often.

1 hour ago, MarcusAseth said:

I don't fully understand this, I basically just needed to pass around width and height so that I can spawn my Entities like so(code below:

Why do you have a Window class then? Why not Tuple<std::string, int, int, Pair<int, int>, bool, bool, ...>? It's the same thing, right?

Seeing "Size" in a function signature conveys useful meaning that seeing "Pair<foo, bar>" does not. It also lets you write interfaces that operate in the language appropriate for the semantics of the data you're sending around - as Alberth noted with his "area" example, but also even simpler than that: what is a size? It's a width and a height. Not a "first" and a "second." Seeing "thing.width" written in code is lot easier to comprehend than "thing.first," just as seeing "Size" in the parameter list is easier to comprehend than "Pair."

##### Share on other sites

ok, so the bottom line is I should either return a Struct filled with info with inside appropriete variables names like WindowInfo.width and WindowInfo.height or split it into two methods Game->GetWindowWidth() , Game->GetWindowHeight()

Did I got this right?

By the way, I added this as well into Utility.h

	template <typename First, typename Second>
using Pair = std::pair<First, Second>;

and I'm glad to see it everything still compiles without any changes required

Edited by MarcusAseth

##### Share on other sites
1 hour ago, MarcusAseth said:

Did I got this right?

Yes.

I'd vote for the first option, since about 80% of the time if you want the width you'll probably also want the height, and there really isn't much to worry about in terms of performance here.

Fixed, thanks

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628707
• Total Posts
2984310
• ### Similar Content

• Hey there,  I have this old code im trying to compile using GCC and am running into a few issues..
im trying to figure out how to convert these functions to gcc
static __int64 MyQueryPerformanceFrequency() { static __int64 aFreq = 0; if(aFreq!=0) return aFreq; LARGE_INTEGER s1, e1, f1; __int64 s2, e2, f2; QueryPerformanceCounter(&s1); s2 = MyQueryPerformanceCounter(); Sleep(50); e2 = MyQueryPerformanceCounter(); QueryPerformanceCounter(&e1); QueryPerformanceFrequency(&f1); double aTime = (double)(e1.QuadPart - s1.QuadPart)/f1.QuadPart; f2 = (e2 - s2)/aTime; aFreq = f2; return aFreq; } void PerfTimer::GlobalStart(const char *theName) { gPerfTimerStarted = true; gPerfTotalTime = 0; gPerfTimerStartCount = 0; gPerfElapsedTime = 0; LARGE_INTEGER anInt; QueryPerformanceCounter(&anInt); gPerfResetTick = anInt.QuadPart; } /////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// void PerfTimer::GlobalStop(const char *theName) { LARGE_INTEGER anInt; QueryPerformanceCounter(&anInt); LARGE_INTEGER aFreq; QueryPerformanceFrequency(&aFreq); gPerfElapsedTime = (double)(anInt.QuadPart - gPerfResetTick)/aFreq.QuadPart*1000.0; gPerfTimerStarted = false; }
I also tried converting this function (original function is the first function below and my converted for gcc function is under that) is this correct?:
#if defined(WIN32) static __int64 MyQueryPerformanceCounter() { // LARGE_INTEGER anInt; // QueryPerformanceCounter(&anInt); // return anInt.QuadPart; #if defined(WIN32) unsigned long x,y; _asm { rdtsc mov x, eax mov y, edx } __int64 result = y; result<<=32; result|=x; return result; } #else static __int64 MyQueryPerformanceCounter() { struct timeval t1, t2; double elapsedTime; // start timer gettimeofday(&t1, NULL); Sleep(50); // stop timer gettimeofday(&t2, NULL); // compute and print the elapsed time in millisec elapsedTime = (t2.tv_sec - t1.tv_sec) * 1000.0; // sec to ms elapsedTime += (t2.tv_usec - t1.tv_usec) / 1000.0; // us to ms return elapsedTime; } #endif Any help would be appreciated, Thank you!

• Hi, I'm building a game engine using DirectX11 in c++.
I need a basic physics engine to handle collisions and motion, and no time to write my own.
What is the easiest solution for this? Bullet and PhysX both seem too complicated and would still require writing my own wrapper classes, it seems.
I found this thing called PAL - physics abstraction layer that can support bullet, physx, etc, but it's so old and no info on how to download or install it.
The simpler the better. Please let me know, thanks!

• It comes that time again when I try and get my PC build working on Android via Android Studio. All was going swimmingly, it ran in the emulator fine, but on my first actual test device (Google Nexus 7 2012 tablet (32 bit ARM Cortex-A9, ARM v7A architecture)) I was getting a 'SIGBUS illegal alignment' crash.
My little research has indicated that while x86 is fine with loading 16 / 32 / 64 bit values from any byte address in memory, the earlier ARM chips may need data to be aligned to the data size. This isn't a massive problem, and I see the reason for it (probably faster, like SIMD aligned loads, and simpler for the CPU). I probably have quite a few of these, particular in my own byte packed file formats. I can adjust the exporter / formats so that they are using the required alignment.
Just to confirm, if anyone knows this, is it all 16 / 32 / 64 bit accesses that need to be data size aligned on early android devices? Or e.g. just 64 bit size access?
And is there any easy way to get the compiler to spit out some kind of useful information as to the alignment of each member of a struct / class, so I can quickly pin down the culprits?
The ARM docs (http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.faqs/ka15414.html) suggest another alternative is using a __packed qualifier. Anyone used this, is this practical?
• By Josheir
In the following code:

Point p = a[1]; center of rotation for (int i = 0; I<4; i++) { int x = a[i].x - p.x; int y = a[i].y - p.y; a[i].x = y + p.x; a[i].y = - x + p.y; }
I am understanding that a 90 degree shift results in a change like:
xNew = -y
yNew = x

Could someone please explain how the two additions and subtractions of the p.x and p.y works?

Thank you,
Josheir
• By alex1997
Hey, I've a minor problem that prevents me from moving forward with development and looking to find a way that could solve it. Overall, I'm having a sf::VertexArray object and looking to reander a shader inside its area. The problem is that the shader takes the window as canvas and only becomes visible in the object range which is not what I'm looking for..
Here's a stackoverflow links that shows the expected behaviour image. Any tips or help is really appreciated. I would have accepted that answer, but currently it does not work with #version 330 ...

• 23
• 10
• 9
• 13
• 13