CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2015
    Posts
    500

    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

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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)

  3. #3
    Join Date
    May 2015
    Posts
    500

    Re: template deduction

    Thanks a lot kaud

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
  •  





Click Here to Expand Forum to Full Width

Featured