type_safe_ptr - Idea for Type-Safe void* Pointer

13:33
Sat
21
Nov 2015

type_safe_ptr - Idea for Type-Safe void* Pointer

I was thinking recently about passing raw data pointers (like void* or char*), which we have to do sometimes in C++. Is there any way to check if the type that we cast it to is the same as the type of last assigned value? I came up with an idea of implementing a "smart pointer" class using RTTI (specifically typeof operator) to store type information next to the actual pointer. Example usage:

int i = 123;
type_safe_ptr ptr{&i};
int j = *ptr.get_typed<int>(); // OK
float f = *ptr.get_typed<float>(); // Error

Initially I wanted to store pointer to const type_info struct returned by typeid operator, and it seems to work in Visual Studio 2015, but language standard defines the object returned by typeid as temporary, so it is not formally correct. Finally I decided to store typeid(T).hash_code(). You can find my implementation of classes type_safe_ptr and type_safe_const_ptr in file: type_safe_ptr.hpp. Here is example tesing program:

#include <cstdio>
#include "type_safe_ptr.hpp"

int main()
{
    type_safe_ptr ptr1;
    // ptr1 is null.
    assert(!ptr1);
    assert(ptr1.get() == nullptr);

    int i = 123;
    type_safe_ptr ptr2{&i};
    // ptr2 is pointer to int.
    assert(*ptr2.get_typed<int>() == 123);
    // It would activate assert inside type_safe_ptr.get_typed, because ptr2 is int not float.
    //assert(*ptr2.get_typed<float>() == 123.f);

    struct STest { int i; } obj;
    // itr2 is now pointer to STest.
    ptr2 = &obj;
    ptr2.get_typed<STest>()->i = 124;
    assert(obj.i == 124);
    // It would activate assert inside type_safe_ptr.get_typed, because ptr2 is now STest not int.
    //assert(*ptr2.get_typed<int>() == 123);
    
    type_safe_const_ptr cptr = type_safe_const_ptr(ptr2);
    // cptr is pointer to const STest.
    assert(cptr.get_typed<STest>()->i == 124);

    const int* constIntPtr = &i;
    cptr.reset(constIntPtr);
    // cptr is now pointer to const int.
    assert(*cptr.get_typed<int>() == 123);
}

Some issues and open question regarding my solution are:

Final question is, whether this whole idea of "type-checking void* smart pointer" makes any sense? I am not sure about that, but anyway it was a funny experiment :)

Comments (1) | Tags: c++ visual studio | Author: Adam Sawicki | Share

Comments

yll
2016-03-25 03:26:20
If http://www.christian--louboutin.in.net/ you http://www.chiflatiron.net.co/ are http://www.ralph-laurens.org.uk/ happy http://www.longchamp.com.de/ temperament, http://www.adidas.org.es/ I http://www.lululemoncanada.ca/ have a http://www.longchamp.us.org/ song, http://www.horloges-rolex.nl/ dance with http://www.prada-shoes.com.co/ your favorite http://www.airmax-2015.org/ look; http://www.christian-louboutinshoes.com.co/ if http://www.pumaonline-shop.de/ you http://www.nike.org.es/ rife, I will http://www.michaeljordan.com.de/ become http://www.oakley--sunglasses.com.au/ a http://www.swarovski--uk.me.uk/ cloud, http://www.abercrombie-fitchsale.com/ falling http://www.p90xworkout.us.com/ some http://www.christianlouboutinshoesoutlet.org/ peerless http://www.ralphlaurenoutlet.us.com/ situation; http://www.nikestore.com.de/ for this flower http://www.instylers.us.com/ Dimei under http://www.cheapjerseys.com.co/ Xu, Xu http://www.pandora-charms-canada.ca/ next stop for http://www.adidasshoes.top/ this http://www.ralphs-lauren.co.uk/ cloud, http://www.ralphlaurenpolos.in.net/ let that http://www.mcmhandbags.com.co/ drop http://www.nikefactory.org/ of http://www.nike-shoesoutlet.us.com/ rain touched that http://www.hollister.us.org/ masterpiece http://www.newbalance-shoes.org/ encounter; not http://www.mmoncler-outlet.com/ to ask, http://www.thenorthfacejackets.net.co/ in http://www.ugg-boots.us.org/ the southern misty http://www.giuseppezanotti.com.co/ rain ink, http://www.montblanc--pens.in.net/ or http://www.nikefree-run.org.uk/ vegetation http://www.louboutin.jp.net/ desolate http://www.northfaceoutlet.com.co/ Saibei http://www.nike-air-max.ca/ only http://mcmbackpack.co-om.com/ hope that http://www.thenorth-faces.co.uk/ you http://www.nike-shoes-canada.ca/ read in conjunction http://www.pandorajewelry.top/ rain http://www.burberrysoutlet2016.com/ Partly http://www.beatsbydre.com.co/ farming, http://www.nikemercurial.in.net/ leisure http://www.burberryonlineshop.de/ together http://www.burberry-handbagsoutlet.com.co/ Immersió http://www.ugg-boots.ca/ listen to http://www.bcbg-max-azria.ca/ the http://www.louis-vuitton-australia.com.au/ birds, http://www.nfl-jersey.us.com/ over http://www.zxcoachoutlet.com/ the http://www.burberrybags-outlet.com/ days together with http://www.michael-kors.net.co/ ordinary water http://www.air-huarache.co.uk/ .

If grinding moonlight, the http://www.calvin-kleins.us.com/ wind pen, next http://www.longchamp-handbags.us.com/ month http://www.edhardy.us.org/ before http://airmax.shoesoutlet.us.com/ it http://www.truereligion-outlet.us.org/ flowers, http://www.nikerosherun.us/ pen http://www.raybans-sunglassesoutlet.in.net/ years http://www.toryburchsale.com.co/ water http://www.abercrombie-and-fitch.us.com/ volume; http://www.moncler-outlet.us.org/ with traces http://www.michael-kors-australia.com.au/ breeze http://www.designer-handbagsoutlet.us.com/ atmosphere, http://www.chanel-bags.com.co/ clouds, and http://northfaceoutlet.co-om.com/ in a http://www.pandorajewellery.com.au/ paper http://www.tommy-hilfiger-canada.ca/ by the recollection of http://www.tiffany-jewelry.net/ old http://www.nike-schoenen.co.nl/ books you warm; http://www.beats-by-dre.com.co/ make clear water http://www.swarovskicanada.ca/ in http://www.lauren-ralph.co.uk/ Qiannian http://www.ray--ban.ca/ landscape http://www.thomas-sabo.com.de/ outside, http://www.juicycoutureoutlet.net.co/ waiting for http://www.burberry-handbags2016.in.net/ the http://www.iphone-cases.net.co/ moon http://www.hollisterclothingstore.org/ and http://www.converse-shoes.net/ new moon; http://www.converse.net.co/ quietly http://www.rayban.org.es/ spend http://www.hollisters-canada.ca/ the http://www.chi-flatiron.us.com/ joy http://www.louisvuitton-outlets.us/ of http://www.canada-goosesjackets.us.com/ silence, http://www.new-balance.ca/ do http://www.swarovski-online-shop.de/ not http://www.salomon-schuhe.com.de/ ask http://www.omegarelojes.es/ how changes http://www.jordanretro.org/ of http://www.ugg-uggboots.net/ marshes, http://www.pandora.com.de/ how http://www.rolex-watch.me.uk/ rotary years, http://www.harrods-london.co.uk/ the only http://www.tomsoutletonline.net/ place http://www.guccishoes.in.net/ in http://www.chanelhandbags.net.in/ the bridges http://www.rolexwatchesforsale.us.com/ for you http://www.cheapuggboots.us.com/ as http://www.tiffanyandco-canada.ca/ I http://www.louis-vuittonoutletcanada.ca/ ended http://www.tommyhilfiger.net.co/ classic.

Life http://www.michaelkors.co.nl/ would http://www.oakleysunglasses-cheap.in.net/ like http://www.coco-chanel.com.de/ to http://www.uggsoutlet.com.co/ make http://www.michaelkorsbags.us.org/ the http://www.barbour-factory.com/ dust http://www.abercrombieand-fitch.ca/ of a lotus, http://www.ralphlaurenoutlet-online.us.com/ the mountain http://www.supra-shoes.org/ waiting http://www.michael-korshandbags.org.uk/ for http://supra.shoesoutlet.us.com/ a http://www.guccishoes.com.co/ ride http://www.truereligion-outlet.com.co/ a http://www.replica-watches.com.co/ water http://www.nike-schuhe.com.de/ ride http://www.hermesbirkin-bag.net/ with http://nike.shoesoutlet.us.com/ a http://www.coach-purseoutlet.net/ window http://www.tocoachoutlet.com/ the http://www.tommy-hilfiger-online-shop.de/ bright http://www.ghd-hairstraightener.net/ window, http://www.oakleyoutlet-online.us.com/ the http://www.swarovski-australia.com.au/ sun http://www.converse-shoesoutlet.com/ was http://coach.euro-us.net/ warm embrace, Wan http://www.cheapjerseys.us.org/ Wan http://www.timberlandbootsoutlet.us.com/ in a http://www.bottega-veneta.us.com/ new Green http://www.adidas-schuhe-online.de/ Mile, http://www.christianlouboutin.org.uk/ this http://www.marcjacobs.us.com/ life filled http://www.uhren-shop.com.de/ with http://www.handbagsoutlet.net.co/ love; http://www.michaelkors-uk.org.uk/ make http://www.weddingdressesuk.org.uk/ a http://monsterbeats.co-om.com/ colorful http://www.basketballshoes.com.co/ in http://www.soccer-shoes.us.com/ full http://www.rayban-sunglasses.us.org/ bloom http://www.louisvuitton.so/ throughout http://www.abercrombie-fitchs.us.com/ the http://www.fashionclothes.us.com/ spring, to http://www.abercrombie-hollister.nl/ meet http://www.prada.com.de/ a http://www.adidas.com.se/ beautiful painting on http://www.nike-free-run.de/ the successful!

A http://www.uggs-onsale.net/ tea http://www.toryburch-sandals.in.net/ seats http://www.salvatoreferragamo.in.net/ bed, http://www.newbalance.com.es/ a pillow http://www.michael-kors-canada-outlet.ca/ Scrolls fine ponder. http://www.jordan-shoes.com.co/ This http://www.nike-air-force.de/ is http://www.adidas-superstar.de/ a http://www.beats-headphone.com.co/ simple http://www.reebok.com.de/ literature, http://www.new-balance-schuhe.de/ this http://www.vans-schuhe.com.de/ is the http://airmax.misblackfriday.com/ classic http://www.hollisteronlineshop.com.de/ psychic, http://www.ferragamoshoes.in.net/ regardless http://www.retro-jordans.com/ of http://www.nikeskos.dk/ gem http://www.kate-spade.gb.net/ material, http://www.michaelkors-outlet-online.com.co/ regardless http://www.bebeclothing.in.net/ of http://www.the-northface.net.co/ the http://www.cheapshoes.com.co/ net http://www.airjordans.us/ worth http://www.michael-kors-outlet.us.org/ of the http://www.coach-factory.in.net/ poor, http://www.pandora-charms.org.uk/ because http://www.nike-air-max.com.de/ the http://www.designerhandbagsoutlet.net.co/ share of hazy http://www.cheap-baseballbats.net/ love, http://www.celine-bags.org/ love http://www.ugg-australia.com.de/ deep http://www.gucci-taschen-outlet.de/ into http://www.kate-spade.in.net/ the http://www.cheaprayban.com.co/ soul.

This literary love, love this http://airhuarache.shoesoutlet.us.com/ millennia-old http://www.cheapmichaelkors.us.org/ Chinese http://www.oakley.com.de/ soul of the http://www.polo-ralph-lauren.de/ wind. http://www.pumashoes.in.net/ Or http://www.michaelkorsoutletonline-sale.us.com/ vigorous, soul-stirring, http://www.hollister-clothing.in.net/ like Longyou http://www.burberry2016.co.uk/ song http://www.michael--kors.us.com/ marsh, Huang http://www.ralphlaurenonlineshop.de/ Fei http://www.barbour-jacketsoutlet.com/ nine http://www.burberry-outletstore.net/ days; http://www.nikefree5.net/ or single elevation grid, http://www.louisvuitton-outlet.com.co/ noble http://www.tomsshoes-outlet.us.com/ Aoshuang, http://coachoutlet.euro-us.net/ like http://www.coachhandbags2016.us.com/ Pine http://www.asicsgels.de/ Valley http://www.tommyhilfigeroutlet.in.net/ Health, Xia http://www.nike-air-max.com.se/ Ying http://www.burberryoutlet-canada.ca/ Cheng http://www.maccosmetics.net.co/ Tong; http://www.nikefree-run.net/ or http://www.bcbg-dresses.com/ want http://www.nikeair-max.es/ to http://www.coach-factoryoutlet.net.co/ go think, therefore, http://www.vans-shoes.co.uk/ become http://www.swarovski-crystal.us.com/ lazy http://www.rayban-wayfarer.in.net/ Ke Zhou, Shen http://www.toms-shoesoutlet.net/ round like http://www.abercrombie-kids.us.com/ the moon and http://www.maccosmetics.gr.com/ stars, the http://www.toms-outlets.us.com/ rivers http://www.cheap-oakleyglasses.us.com/ into the http://www.guccishoes.us.org/ ocean; or http://www.ugg-bootsclearance.com/ plaintive http://www.mk-outlet.us.com/ blurred, cold http://www.ralphslauren-outlet.co.uk/ hesitation, http://www.adidas-superstar.nl/ like Su Yi http://www.omega-watches.us.com/ impermanence, cold http://www.timberlandshoes.net.co/ feast http://www.uggs.in.net/ next http://www.toms-shoes.com.co/ month. Let him http://www.newoutletonlinemall.com/ how http://www.nike-air-max.com.au/ Mind, http://www.burberry-outlets.co.uk/ only http://www.ray-ban-outlet.us.com/ for http://www.uggboots.com.de/ the http://www.tnf-jackets.us.com/ love affair http://www.christianlouboutinoutlet.net.co/ Everlasting, http://www.michaelkors.com.se/ the text http://www.coachoutletstore-online.com.co/ from http://www.the-north-face.ca/ the heart http://www.newbalance-outlet.org/ Think http://www.ralphlaurenepolo.com/ that http://www.yoga-pants.net.co/ summer, http://www.tiffany-andco.com.au/ a http://www.toms-shoes.net.co/ column http://www.juicycouture.com.co/ Chen http://www.asics-outlet.us.com/ Xiang http://www.beatsbydrdre.co.com/ a cool pillow, http://www.burberryoutlet2016.us.com/ which http://www.nikeshoesoutlet.org.uk/ is http://www.polo-outlets.com.co/ the roots of http://www.thenorthface.com.de/ what http://www.bottegaveneta-bagsoutlet.com/ you http://www.the-northfacejackets.us.com/ need http://www.thejoreseproject.com/ to http://www.nike-skor.com.se/ make http://www.canadagooses-2016.com/ tea http://www.oakley-outletstore.in.net/ cooking http://www.longchamp-handbagsoutlet.us.com/ wine, http://www.jimmy-choos.com/ Hongxiutianxiang http://www.adidas--canada.ca/ just http://www.tiffanyandco.net.co/ hand poetry http://www.air-max.com.de/ twos http://www.cheapoakley-sunglasses.com/ Bliss http://www.ralph-laurenoutlet.ca/ half a day http://www.nikestore.us/ can tolerate http://www.mkoutletonline.us.com/ you wander. http://www.rayban.com.de/ Static http://www.mcmsworldwide.com/ water body, http://www.mcm-bags.in.net/ Shen soul http://www.rosherun.org.uk/ dream, http://www.polooutlets-store.com/ it http://www.mcmbags.net/ is http://www.uggboots.net.co/ only http://www.toms--outlet.com.co/ a http://www.tiffany-und-co.de/ few http://www.converse.com.de/ clouds color http://www.louis-vuittonblackfriday.com/ country http://www.oakley-sunglasses.mex.com/ Pitt, http://www.woolrich-clearance.com/ rivers http://www.louisvuitton.jp.net/ and mountains, http://www.nike-roshe-run.com.es/ jade http://www.burberry-outlet.net.co/ group. http://www.coach-factoryyoutletonline.net/ Do http://www.thomas-sabos.org.uk/ not http://www.hermes-outlet.com.co/ ask bustling http://www.pradaoutlet.com.co/ past, http://www.swarovskijewelry.com.co/ gone; http://www.nike-air-max.us/ do http://www.insanity-workout.us.com/ not http://www.toryburchoutlet-sale.us.com/ ask http://www.eyeglassframes.in.net/ flowery http://www.hermesbags.jp.net/ Meijuan, Homecoming, http://www.ugg-boots-australia.com.au/ heart http://www.michael-kors.com.co/ like hanging http://www.oakleys-glasses.us.com/ a http://www.michael-kors.com.es/ mirror, http://www.guccihandbags.net.co/ according to http://www.tory-burchshoesoutlet.net/ Italian Bodhi http://www.babyliss-pro.us.com/ station.

Three thousand http://www.tommy-hilfiger.com.de/ Red http://www.coco-chanels.us.com/ ink, http://www.longchamphandbagsoutlet.net/ as http://www.coachoutletstore.net.co/ are cold-blooded bustling http://uggaustralia.misblackfriday.com/ paper, into http://www.prada-handbags.com.co/ a http://www.true-religion.com.co/ word http://www.raybans.us.org/ discretion. Five thousand years http://www.michael-kors-outlet-online.us.org/ of calligraphy http://www.gucci-outletstore.com/ love http://www.replica-handbags.in.net/ this, http://www.tommy-hilfigeroutlet.com/ love http://www.hogan.com.de/ flames smoke sand http://www.airhuarache.co.uk/ which http://www.ferragamo.com.co/ contained http://www.oakley-sunglasses-canada.ca/ five http://www.hollister-abercrombie.com.se/ thousand http://www.cheapoakley-sunglasses.in.net/ years. Regardless of the http://www.lululemonoutlet.gb.net/ time, http://www.rayban-sunglassesoutlet.co.uk/ and http://www.vans-shoes.net/ regardless http://www.longchampoutlet.com.co/ of http://www.michaelkorsoutlet-online.ar.com/ age just http://www.louis-vuitton-taschen.com.de/ enthusiastic, http://www.yoga-pants.ca/ you http://www.jimmy-chooshoes.com/ can play http://www.oakley.org.es/ the http://www.outlet-burberry.net.co/ most beautiful music in the http://www.coachblackfriday.com/ soul. Born This http://www.louisvuittons.com.co/ paper declared the era http://www.nike-huarache.nl/ of http://www.truereligionjeans.net.co/ thick http://www.michael-kors-taschen.com.de/ ink, it http://www.cheap-raybans.com/ is http://www.longchamp.com.co/ really Qing http://www.ralph-lauren.com.au/ Sansei http://www.louis--vuitton.org.uk/ lucky, http://www.cheap-jordans.net/ I have http://www.christian-louboutin-shoes.ca/ the edge. http://www.rosheruns.us/ Not to think about what flat http://www.poloralphlaurenoutlet.net.co/ Zeze, http://www.guccioutlet-sale.in.net/ do not http://www.ralphlaurenoutletonline.in.net/ repair http://www.christianlouboutinshoes.jp.net/ what http://www.lululemon-australia.com.au/ suppression http://www.nike-rosherun.nl/ suppression triumphantly, http://www.oakleys-sunglasses.us.com/ play http://www.cheap-michaelkors.com/ with http://www.wedding--dresses.ca/ my http://www.nike-roshe-run.de/ ink, paper http://www.the-northface.com.co/ and http://www.pradahandbags.net.co/ ink, http://www.coachoutlet-online.com.co/ ink http://www.michael-kors-handbags.us.com/ into http://www.airmax-90.org/ song. http://www.levisjeans.com.co/ Wenbujiadian, http://www.tomsoutlet-online.com/ Tengjiao http://www.giuseppe-zanotti.net/ Qifeng. http://www.rayban.co.nl/ Three http://www.pulseras-pandora.com.es/ feet http://www.oakley-sunglassoutlet.net/ of white Xuan, http://www.mizuno-running.net/ lead me http://www.ralphlaurenoutletonline.us.org/ thousand http://www.jordanrelease-dates.us.com/ words http://www.cheap-nike-shoes.net/ mantra; five http://michaelkors.co-om.com/ bucket http://www.tommy-hilfiger.co.nl/ heavy http://www.burberry-outletonlinesale.in.net/ color, http://www.uggsaustralia.com.co/ any http://www.rolex-watches.us.com/ time http://www.michaelkorsoutlet.ar.com/ I http://www.eyeglassesonline.us.com/ wanton http://www.burberryoutlet-sale.in.net/ chapters. Wenqi thunder, http://www.soft-ballbats.com/ put pen http://www.michaelkors.so/ to paper http://www.uggs.co.nl/ to http://www.guccishoes-uk.co.uk/ stay http://michaelkors.euro-us.net/ hui, was http://www.rolexwatches-canada.ca/ widely bully Zimei, bearing http://www.coach-outletonline.net.co/ pressure http://www.katespadeoutlet.gb.net/ Bai.

Post comment

Nick *
Your name or nickname
E-mail
Your contact information (optional, will not be shown)
Text *
Content of your comment
Calculate *
(* - required field)
STAT NO AD [Stat] [Admin] [STAT NO AD] [pub] [Mirror] Copyright © 2004-2016 Adam Sawicki
Copyright © 2004-2016 Adam Sawicki