Thread: Template <typename T> and class constructor question

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    227

    Exclamation Template <typename T> and class constructor question

    Hi there!

    Getting a little further on with my studies and I've come across something new(ish) again. I'm pretty sure I understand it but a second opinion is always nice. Have a look at the code snippet below:

    Code:
    template<typename T>
    
    class UploadBuffer
    {
    
    public:
        UploadBuffer(ID3D12Device* device, UNIT elementCount, bool isConstantBuffer)
        : mIsConstantBuffer(isConstantBuffer)
        {
    
            ....skipped some stuff
    
            if(isConstantBuffer)
            {
                mElementByteSize = d3dUtil::CalcConstantBufferByteSize(sizeof(T));
    
        ....more class stuff continues
    The details aren't really important for this question, in case you're interested this is a class that creates a buffer for the CPU to write into which is then later read by the GPU. The byte size parts are due to the fact that (for whatever reason) if you're creating a constant buffer all data needs to be in multiples of 256 bytes. So even say a 4x4 matrix at 64 bytes gets 'promoted' to 256 bytes as per the protocol requirements. The constructor just checks a bool loaded into the constructor function to see if this is needed or not. AFAIK you don't have to do that for a vertex or index buffer.

    Anyways that's API specific stuff which isn't what I'm here for. I'm here to ask if the template allows for different data types to be used inside the class? Sometimes the size of the data it receives in the constructor varies, it depends what you're using the constant buffer for.

    Getting a bit ahead of myself here perhaps I should now post the code that creates the class:

    Code:
    std::unique_ptr<UploadBuffer<ObjectConstants>> mObjectCB = nullptr;
    mObjectCB = std::make_unique<UploadBuffer<ObjectConstants>>(md3dDevice.Get(), n, true);
    So I presume those two lines are just preparing a pointer in some way, and then calling the class constructor whilst at the same time telling the class what type/size of variable T is going to be.

    That would make sense as the ObjectConstants struct can vary in size depending upon what you're doing with the graphics.

    Hope that made sense, thanks!!

  2. #2
    Registered User
    Join Date
    Sep 2024
    Posts
    1
    Quote Originally Posted by shrink_tubing View Post

    Hi there!

    Anyways that's API specific stuff which isn't what I'm here for. I'm here to ask if the template allows for different data types to be used inside the class? Sometimes the size of the data it receives in the constructor varies, it depends what you're using the constant buffer for.
    Hello.

    If you're passing in the data via a template then yes.

    The template you use while constructing the object should however support all the methods and operators that your code calls and uses respectively.

    In your particular case, it seems 'elementCount' is the size of the buffer which gets ignored if the buffer is of type 'Constant'.

    Template errors are compile time errors which shouldn't be a problem while working with hardware.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    227

    Thumbs up

    Nice! Thanks for your reply

    p.s. element count AFAIK doesn't get ignored if it's a constant buffer. In the case of a buffer being a 'constant buffer' element count is needed to help determine the entire size of the buffer, as each entry has to be a multiple of 256 bytes.
    Last edited by shrink_tubing; 1 Day Ago at 12:25 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. template <typename T=void> ??
    By cpjust in forum C++ Programming
    Replies: 5
    Last Post: 03-25-2009, 06:36 AM
  2. Template class constructor
    By DL1 in forum C++ Programming
    Replies: 3
    Last Post: 01-22-2009, 07:33 AM
  3. Conversion constructor for template class
    By MarkZWEERS in forum C++ Programming
    Replies: 19
    Last Post: 05-08-2008, 01:22 PM
  4. typename in class template
    By l2u in forum C++ Programming
    Replies: 4
    Last Post: 04-02-2008, 04:22 PM
  5. template class default constructor problem
    By kocika73 in forum C++ Programming
    Replies: 3
    Last Post: 04-22-2006, 09:42 PM

Tags for this Thread