1

I am using boost 1.45.0 and have some code that does the following:

template <typename T = some_type, std::size_t N = 3> class my_class { public: typedef T value_type; ... ... my_class(value_type i0) {BOOST_STATIC_ASSERT(N==1); m_data[0]=i0;} protected: T m_data[N]; //!< The internal data array used to store indices } 

This generates the following error on MS VC++ 2010 (which I understand has implemented static_assert as one of their major changes) and no errors on MS VC++ 2008:

 error C2338: N==1 

Likewise, there are other errors at some other BOOST_STATIC_ASSERTs in the same code (left out for brevity).

I also tried replacing with the static_assert from VC++ but get a similar build error (prints out the message string).

What workaround exists for this?

6
  • What's the code that instantiates func that should trigger the error? Commented Nov 1, 2011 at 22:14
  • 5
    Well, N doesn't equal 1, so what do you expect? Commented Nov 1, 2011 at 22:22
  • static_assert fails when the condition is false, not when it is true. Maybe that's where your confusion comes from? Just to be clear, 3==1 is false, hence it fails. Commented Nov 1, 2011 at 22:27
  • thanks @GMan: I see your point; but somehow I feel there might be a fix to this. Isn't this type of usage covered by SFINAE? Plus it builds fine on MS VC++ 2008 (albeit I was using boost 1.39.0 then) Commented Nov 1, 2011 at 22:31
  • The static assert is doing what it should be. You're checking that N == 1, but you set its default value to 3. So if any my_class instance with a default template paramater comes along, it should trigger that assertion. What are you trying to do? Commented Nov 1, 2011 at 22:34

1 Answer 1

2

I think you may be misunderstanding the purpose of static assertions. Static assertions are meant to state properties that must be true for the code to compile. If what you want is to write a function that won't be generated unless some condition is true but not fail compilation, you need to use SFINAE.

// don't forget to #include <type_traits> for std::enable_if template <std::size_t N1 = N> my_class(value_type i0, typename std::enable_if<N1==1>::type* = 0) {m_data[0]=i0;} 
Sign up to request clarification or add additional context in comments.

9 Comments

@Fernandes.. you might be on to something. Let me try this and get back with you. I think you nailed it.
@Fernandes, I tried this out but now I am getting a build error: error C2039: 'type' : is not a member of 'std::tr1::enable_if<_Test>'
@user545591 : That's what happens if N != 1 and you have no alternative constructor overloads to call. I.e., the whole purpose is to make the overload containing the enable_if ineligible during overload resolution if the condition is false, so you need some other overload to call in that circumstance.
Oh, right, I forgot something. Seems like you need to make the constructor a template for this to work. I'll edit.
@R.MartinhoFernandes : He's using VC++ 2010, which does not support default function template arguments, so your code as-is won't work.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.