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 91723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:04:28+00:00 2026-05-10T23:04:28+00:00

I want to align my member variables based on a class template type but

  • 0

I want to align my member variables based on a class template type but I’m not sure if it is actually possible.

The following is a (very) simple example of what I’d like to do

template<int Align> class MyClass { private:   struct MyStruct   {     // Some stuff   } __declspec(align(Align));    __declspec(align(Align)) int myAlignedVariable; }; 

So what I’d like is for Align to be a per-instance variable, and only through that is the align value of the class contents decided.

Unfortunately I always get the following error

error C2975: 'test::MyClass' : invalid template argument for 'Align', expected compile-time constant expression 

So, is this actually possible or can the alignment only be possible using a fixed compile time constant? If not, can anyone think of a way around this?

Thanks 🙂

  • 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. 2026-05-10T23:04:29+00:00Added an answer on May 10, 2026 at 11:04 pm

    Custom alignment isn’t in the standard, so how the compilers deal with it is up to them – looks like VC++ doesn’t like combining templates with __declspec.

    I suggest a work-around using specialisation, something like this:

    template<int A> struct aligned; template<> struct aligned<1> { } __declspec(align(1)); template<> struct aligned<2> { } __declspec(align(2)); template<> struct aligned<4> { } __declspec(align(4)); template<> struct aligned<8> { } __declspec(align(8)); template<> struct aligned<16> { } __declspec(align(16)); template<> struct aligned<32> { } __declspec(align(32)); 

    and then derive from that in your code:

    template<int Align> class MyClass { private:   struct MyStruct : aligned<Align> {     // stuff   }; }; 

    This unfortunately breaks the POD-ness of MyStruct. It also doesn’t work with built-in/existing types, so you’ll have to use a wrapper for those.

    aligned_t<int, 4> myAlignedVariable; 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 86k
  • Answers 86k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You could have something like this: <ItemTemplate> <tr> <td colspan="2"><%#… May 11, 2026 at 5:18 pm
  • Editorial Team
    Editorial Team added an answer from BeautifulSoup import BeautifulSoup import re html_text = """ <h2>this… May 11, 2026 at 5:18 pm
  • Editorial Team
    Editorial Team added an answer [[UIDevice currentDevice] uniqueIdentifier] May 11, 2026 at 5:18 pm

Related Questions

I'm creating (really, re-creating) an app that has existing user and other data in
I'm using a the TreeView control and it scrolls automatically to left-align TreeViewItem when
I have a multiline text string (e.g. Stuff\nMore Stuff\nYet More Stuff), and I want
I have a problem in my code. I want to get the b tag

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.