Conditional assignment ?:
I''m just wondering if anyone ever uses this conditional assignment. I just learned about it today and want to know if it is ever used in complex programs.
It''s a convenient replacement for a single if/else statement.
The real point is that it returns a value, while if/else doesn''t (directly).
I do use it extensively in my programs, I''m sure many do.
-Nik
The real point is that it returns a value, while if/else doesn''t (directly).
I do use it extensively in my programs, I''m sure many do.
-Nik
Some company uses it.. some don''t... Just remember what it does in case you see it in the code.. Personally I don''t use it.. but that''s just me.. I am pretty sure the equilvanlency if statement is just as good as ?: in terms of performance.
the only time I use it is to replace statements like this
with something like this
you can definately get carried away with the conditional, but in this case, I think it''s justified.
if(somecondition) y=something;else y=somethingelse
with something like this
y = somecondition ? something : somethingelse;
you can definately get carried away with the conditional, but in this case, I think it''s justified.
It''s conditional expression or sometimes the ternary operator, not conditional assignment. You don''t need to assign it''s result.
I use it when the whole expression will still be sufficiently short.
It can also be used on left side of assignments, like this:
But I have never seen this form used in real code.
foo(x < y ? 5 : 0);
I use it when the whole expression will still be sufficiently short.
It can also be used on left side of assignments, like this:
int x=0, y=0; (true ? x : y) = 4; //set value of x (false ? x : y) = 5; //set value of y cout << x << " " << y << endl; //prints 4 5
But I have never seen this form used in real code.
You can use it in initializer lists, which is kind of cool. I saw that on here about two weeks ago or so.
edit: man I am tired.
[edited by - antareus on January 4, 2004 3:46:36 PM]
edit: man I am tired.
[edited by - antareus on January 4, 2004 3:46:36 PM]
bah...
amateurs
amateurs
// select array where we will perform searchconst DXDeviceState *pSearchState = (type == EDS_Render) ? m_pRenderStates : (type == EDS_Sampler) ? m_pSamplerStates : (type == EDS_Texture) ? m_pTextureStates : 0;DWORD dwSearch = (type == EDS_Render) ? m_dwNumRenderStates : (type == EDS_Sampler) ? m_dwNumSamplerStates : (type == EDS_Texture) ? m_dwNumTextureStates : 0;
ffx: I guarantee that's slower than the equivalent if-else lattice. I've written similar things before, and although they're ego-building, they're never faster or more readable once they get that complex. Just a note.
Later,
ZE.
//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links
[edited by - zealouselixir on January 4, 2004 3:59:34 PM]
Later,
ZE.
//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links
[edited by - zealouselixir on January 4, 2004 3:59:34 PM]
Just some my old code. Damn things actually start to slow you down. Never use that kind of ?:
Here is assembly
Nasty ha...
Here is assembly
1915: // select array where we will perform search1916: const DXDeviceState *pSearchState =1917: (type == EDS_Render) ? m_pRenderStates1918: : (type == EDS_Sampler) ? m_pSamplerStates1919: : (type == EDS_Texture) ? m_pTextureStates1920: : 0;10011A6E cmp dword ptr [type],110011A72 jne Engine::CDirect3DEngine::CreateDeviceState+0BDh (10011a82)10011A74 mov ecx,dword ptr [this]10011A77 mov edx,dword ptr [ecx+2D4h]10011A7D mov dword ptr [ebp-38h],edx10011A80 jmp Engine::CDirect3DEngine::CreateDeviceState+0F8h (10011abd)10011A82 cmp dword ptr [type],210011A86 jne Engine::CDirect3DEngine::CreateDeviceState+0D1h (10011a96)10011A88 mov eax,dword ptr [this]10011A8B mov ecx,dword ptr [eax+2D8h]10011A91 mov dword ptr [ebp-3Ch],ecx10011A94 jmp Engine::CDirect3DEngine::CreateDeviceState+0F2h (10011ab7)10011A96 cmp dword ptr [type],310011A9A jne Engine::CDirect3DEngine::CreateDeviceState+0E5h (10011aaa)10011A9C mov edx,dword ptr [this]10011A9F mov eax,dword ptr [edx+2DCh]10011AA5 mov dword ptr [ebp-40h],eax10011AA8 jmp Engine::CDirect3DEngine::CreateDeviceState+0ECh (10011ab1)10011AAA mov dword ptr [ebp-40h],010011AB1 mov ecx,dword ptr [ebp-40h]10011AB4 mov dword ptr [ebp-3Ch],ecx10011AB7 mov edx,dword ptr [ebp-3Ch]10011ABA mov dword ptr [ebp-38h],edx10011ABD mov eax,dword ptr [ebp-38h]10011AC0 mov dword ptr [pSearchState],eax1921: DWORD dwSearch =1922: (type == EDS_Render) ? m_dwNumRenderStates1923: : (type == EDS_Sampler) ? m_dwNumSamplerStates1924: : (type == EDS_Texture) ? m_dwNumTextureStates1925: : 0;10011AC3 cmp dword ptr [type],110011AC7 jne Engine::CDirect3DEngine::CreateDeviceState+112h (10011ad7)10011AC9 mov ecx,dword ptr [this]10011ACC mov edx,dword ptr [ecx+2E0h]10011AD2 mov dword ptr [ebp-44h],edx10011AD5 jmp Engine::CDirect3DEngine::CreateDeviceState+14Dh (10011b12)10011AD7 cmp dword ptr [type],210011ADB jne Engine::CDirect3DEngine::CreateDeviceState+126h (10011aeb)10011ADD mov eax,dword ptr [this]10011AE0 mov ecx,dword ptr [eax+2E4h]10011AE6 mov dword ptr [ebp-48h],ecx10011AE9 jmp Engine::CDirect3DEngine::CreateDeviceState+147h (10011b0c)10011AEB cmp dword ptr [type],310011AEF jne Engine::CDirect3DEngine::CreateDeviceState+13Ah (10011aff)10011AF1 mov edx,dword ptr [this]10011AF4 mov eax,dword ptr [edx+2E8h]10011AFA mov dword ptr [ebp-4Ch],eax10011AFD jmp Engine::CDirect3DEngine::CreateDeviceState+141h (10011b06)10011AFF mov dword ptr [ebp-4Ch],010011B06 mov ecx,dword ptr [ebp-4Ch]10011B09 mov dword ptr [ebp-48h],ecx10011B0C mov edx,dword ptr [ebp-48h]10011B0F mov dword ptr [ebp-44h],edx10011B12 mov eax,dword ptr [ebp-44h]10011B15 mov dword ptr [dwSearch],eax
Nasty ha...
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement