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

  • SEARCH
  • Home
  • 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 1027259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:12:51+00:00 2026-05-16T12:12:51+00:00

Is this portable or at least safe to use with g++? #include <iostream> #include

  • 0

Is this portable or at least safe to use with g++?

#include <iostream>
#include <vector>

struct c {};
std::vector<c*> v;
struct i : c { i () { v.push_back (this); } } a, b, c;

int main () {
  std::cout << v.size () << "\n"; // outputs 3 with g++
}

EDIT:

Ok, what I need turned out to be a bit harder: The same code with templates:

#include <iostream>
#include <vector>

template < typename T > struct c {};
template < typename T > struct cv { static std::vector<c<T>*> v; };
template < typename T > std::vector<c<T>*> cv<T>::v;
template < typename T > struct i : c<T> { i () { cv<T>::v.push_back (this); } };

cv<int> dummy; // even this won't initialize cv<int>::v
i<int> a, b, d;

int main () {
  std::cout << cv<int>::v.size () << "\n"; // outputs 0 :-(
}

How could I fix this to work as above?

EDIT 2:

Here is an ugly fix with macros (I hope there is a better way to do it):

#include <iostream>
#include <vector>

template < typename T > struct c {};
template < typename T > struct cv;
#define INITCV(X) \
  struct v##X { static std::vector<c<X>*> v; }; \
  std::vector<c<X>*> v##X::v; \
  template <> struct cv<X> { typedef v##X V; }
template < typename T > struct i : c<T> { i () { cv<T>::V::v.push_back (this); } };

INITCV(int);
i<int> a, b, d;

int main () {
  std::cout << cv<int>::V::v.size () << "\n"; // outputs 3 again :-)
}

(BTW, should I have posted a new question instead of the edits?)

  • 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-05-16T12:12:52+00:00Added an answer on May 16, 2026 at 12:12 pm

    For your updated question, I haven’t waded through the standard to find out when members of implicitly-instantiated templates are supposed to be initialized, but explicit instantiation does seem to be the solution:

    template class cv<int>; // Not a dummy. Declares the template like a class.
    

    Standardese at 14.7.2/7:

    The explicit instantiation of a class
    template specialization implies the
    instantiation of all of its members
    not previously explicitly specialized
    in the translation unit containing the
    explicit instantiation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

class Test { ; int x; }; Is this perfectly legal and portable?
During this year there will be coming couple sub-600€ multi-touch portable computers that contain
I'm not talking about making portable code. This is more a question of distribution.
class Test { enum{}; ... }; Is this empty enum definition portable? Compiles in
Is this valid/portable/legal code: class Vector3 { public: float& operator [] ( const size_t
I am attempting to do this in a portable version of Django: from django.db
What's the best one-stop-shop safe C library solution on the Mac? I use quotes
Is it possible to actually make use of placement new in portable code when
It seems like using this function would be more portable or reliable, because the
this is my first question in here, and I would like to ask if

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.