• 카테고리

    질문 & 답변
  • 세부 분야

    프로그래밍 언어

  • 해결 여부

    해결됨

static_assert 매개변수 관련 질문

23.02.08 18:14 작성 23.02.08 18:16 수정 조회수 211

0

#include <iostream>
#include <vector>
#include <cassert>

using namespace std;

void printVector(vector<int>& temp, const int index)
{
	assert(index >= 3);
	static_assert(index >= 3, "index should be less than 3");//여기서 컴파일 에러
}

int main()
{
	std::vector<int> vec{ 1,2,3,4,5 };
	
	printVector(vec, 3);
}

static_assert 에서 에러가 나는데

매개변수를 const 상수화 해도 static_assert문에서 에러가 나는 이유가 뭔가요?

답변 1

답변을 작성해보세요.

0

강민철님의 프로필

강민철

2023.02.08

https://cplusplus.com/forum/beginner/115138/

위 링크를 보면 static_assert는 compile time에 수행되기 때문에

컴파일 당시 값을 확정지을 수 없는 값에 대해서는 (런타임에 결정되는값은)

사용할 수 없다고 나와 있습니다.

 

런타임 assertion이 필요하다면 아래를 사용하면 된다고 나와 있네요.

assert() in <cassert> http://en.cppreference.com/w/cpp/error/assert