Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8317117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:30:57+00:00 2026-06-08T21:30:57+00:00

I want to do something like this: template<typename CType, unsigned int targetDimensions> struct GeneratePointerType

  • 0

I want to do something like this:

template<typename CType, unsigned int targetDimensions> struct GeneratePointerType
{
    typedef typename GeneratePointerType<CType, targetDimensions-1>::type* type;
    static const unsigned int dimensions = GeneratePointerType<CType, targetDimensions-1>::dimensions+1;
};
template<typename CType> struct GeneratePointerType<CType, 0>
{
    typedef CType type;
    static const unsigned int dimensions = 0;
};

Dictionary<int, GeneratePointerType<int, valueDimensions>::type >

So basically I want to instantiate a container template for a pointer type, but I don’t know the pointer level of the pointer to create before runtime, so the approach from above of course can’t wocompile, as “dimensions” isn’t a compile-time constant.

Is there a way (that is compatible to pure C++03 without any C++11 only features) to achieve the intended behavior?

EDIT:
“If you could tell us the real problem, we might have a better solution.”
“So to clarify, you want a map of N dimensional array/objects, where N is a runtime value? Wierd. I can’t imagine a use-case”
OK, let me tell you a few words about the usecase:

I have a class Template Dictionary, which is basically just behaving like the C# or Java Dictionary generics.

This class is used in C++ wrapper library around a C library.

Therefor I have conversion functions, that convert data between instances of C++ container classes and instances of C container structs.

As you know, C doesn’t have generics, so while in C++ I can create containers like this:

Dictionary<int, int**> dict;

in C I have to do it like this:

CDictionary dic;
dic.keytype = TYPECODE_INT;
dic.valuetype = TYPECODE_INT;
dic.valueDimensions = 2;

Now when converting a C Dictionary into a C++ Dictionary, I struggle on how to generate the correct number of *’s, as the info stored inside the C dictionary isn’t a compile time constant.

EDIT2:
Actually the point, that I am getting that runtime-dimension-counts from the underlying C interface doesn’t matter, as the C lib creates those structs from serialized data, that comes in over the network and as its impossible to know at compiletime of the lib, how many array-dimensions an array, that comes in over the network from another app in potentially another programming langauge, will have, a C++ implementation of the deserialization would still have to determine valuetypes for Dictionaries from runtime information about the array dimensions.

EDIT3:
“OK, I think you need to illustrate how you want to use this structure, because I clearly didn’t interpret your question correctly, and none of this information is in the question. Can you edit in some pseudocode showing what you’re trying to achieve?”
call to public interface:

Dictionary<int, int*> dic;
int* arr = new int[10];
dic.put(1, arr, a0);
delete arr;

send(dic); // dic gets serialized and sent over the netwowork

When receiving a serialized dic, I want to do deserialize it back, before passing it to a callback:

// read out typecodes and dimnesions from the serialized data
// [...]
// create the Dictionary from that info
switch(valueTypeCode)
{
    case TYPECODE_SHORT:
        return new Dictionary<int, GeneratePointerType<short, valueDimensions>::type>[size]();
    case TYPECODE_INTEGER:
        return new Dictionary<int, GeneratePointerType<int, valueDimensions>::type>[size]();
}
// fill it with the deserialized payload afterwards
// [...]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T21:30:58+00:00Added an answer on June 8, 2026 at 9:30 pm

    If the parameter N isn’t known at compile time, you can’t make it part of your static type. So, you definitely have to use some kind of runtime dispatch.

    Likely options are:

    1. just wrap the C structures shallowly (providing operator overloads and other methods, encapsulating the underlying data) and access it exactly the same way

      class Dictionary {
          CDictionary *inner;
      public:
          // C++ syntactic sugar here
      };
      
    2. if N is bounded above by some reasonable value (eg, you can assume N < 10), you could instantiate a template class for every valid N, which implements an abstract interface. Then your template instantiations are hidden in a translation unit which only exposes a public (virtual) interface and a factory function; actual access uses runtime polymorphism

      class AbstractDictionary {
      public:
          virtual ~AbstractDictionary() = 0;
          // virtual methods
      };
      AbstractDictionary* wrap(CDictionary *);
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have something like this: typedef int AnotherType; template <typename T> Func( T Value
I want to do something like this: template<template<int d, class> class container, int dim
I want to implement is_pointer. I want something like this: template <typename T >
I want to do something like this: template <typename T:public Vertex> addTri( T v1,
Basically, what I want to do is something like this: template<typename x,typename y,typename z>
I wish to do something like this: template<typename ...T> struct foo { bar<0 /*to
I want to do something like this: require 'erb' @var = 'test' template =
I want to do something like this declare @a int=1 if (@a=1) with cte
I'd like to build something like this: File 1: template<typename Vector> namespace myNamespace {
Is there any way to make something like this work: template<typename T, typename X,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.