-
September 8th, 2020, 04:18 AM
#1
template deduction
Hello,
I have following piece of template code,
Code:
void PredictionRaster::AutoSetSetIllegalValueIfRequired(PRED_DATA_TYPE pdt, AIBaseRaster *pRaster)
{
ASSERT(pRaster);
if (!pRaster)
{
return;
}
switch (pdt)
{
case PR_DATA_UCHAR_OVERRIDEFILL:
{
const unsigned char MAX_LOSS = 200;
SetIllegalValue(pRaster, MAX_LOSS);
}
break;
case PR_DATA_USHORT_OVERRIDEFILL:
{
const unsigned short NO_BEAM_INDEX = USHRT_MAX;
SetIllegalValue(pRaster, NO_BEAM_INDEX);
}
case PR_DATA_UCHAR_METRICS_OVERRIDEFILL:
SetIllegalValue<unsigned char>(pRaster, 0);
break;
}
}
template<typename RastType> void PredictionRaster::SetIllegalValue(AIBaseRaster *pRaster, RastType illegalValue)
{
auto pTypedRaster = dynamic_cast<RasterT<RastType>*>(pRaster);
if (pTypedRaster)
{
pTypedRaster->OverrideIllegalValue(illegalValue);
}
}
I dont know how the template param in the red coloured line is being used ?
Could you please explain me ? This is some legacy code
I also have another doubt in same area, may be post later once this is resolved
thanks a lot
thanks
Priya
-
September 8th, 2020, 06:26 AM
#2
Re: template deduction
Code:
SetIllegalValue<unsigned char>(pRaster, 0);
...
template<typename RastType> void PredictionRaster::SetIllegalValue(AIBaseRaster *pRaster, RastType illegalValue)
When SetIllegalValue is used, RastType becomes unsigned char. The type specified between <> in the function call is passed to the templated function as the template type. Often the templated type is deduced from the passed parameters, but in this case the type is explicitly specified.
If just:
Code:
SetIllegalValue(pRaster, 0);
was used, then the type of RastType would be deduced as int as the type of 0 is int.
Last edited by 2kaud; September 8th, 2020 at 06:28 AM.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
September 8th, 2020, 10:59 AM
#3
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|