C++ 템플릿 클래스에서도 explicit 를 쓸 수 있습니까?

hsnks100의 이미지

template
struct test
{
test()
{
_tprintf(_T("TEST"));
}
};

int main(int argc, char* argv[])
{

test<3.5>();
return 0;
}

test<3.5> 처럼 int 형이 아니면 에러내고 싶은데 어떻게 하는건지 궁금합니다.

일단 제 컴파일러인 vs2008 에서는 아무일 없이 컴파일이 됐습니다.

--------------------------------
6월 30일날 입댄데 궁금해 뒤지겠군요;;

klyx의 이미지

템플릿 인자가 안보이네요.
다음과 같이 하면 됩니다.

#include <iostream>
 
template<int n>
void test() {std::cout << n << std::endl;}
 
int main() {
	test<3>();
	test<10>();
	//test<4.5>(); error!
	return 0;
}
hsnks100의 이미지

코드를 코드로 묶질 않았네요.

#include <cstdio>	
#include <TCHAR.H>
template <int T>
struct test
{
 test()
 {
  _tprintf(_T("TEST"));
 }
};
 
int main(int argc, char* argv[])
{
 
 
 test<3.5>();
 return 0;
}

3.5 에서 에러가 나야되는데 컴파일이 되는 이유를 알고 싶습니다. 그리고

좀더 엄격하게 하는 방법도 알고 싶습니다.

----------------------------------------------------
알디 입니다.

----------------------------------------------------
개인 블로그: https://kangssu.com

klyx의 이미지

여전히 템플릿인자가 안보이네요. 제가 적은거처럼 template<int n>라고 했는데도 에러없이 컴파일이 된다는 뜻인가요?
그렇다면 컴파일러에 따른 차이로 보이네요.
코드를 보니 VC++식의 매크로가 보이는데, 전 g++로 컴파일 했을때 컴파일에러가 발생했습니다.
어느쪽이 표준인지는 모르겠네요.
템플릿 메타프로그래밍을 하시는게 아니라 단지 엄격한 형검사가 필요한거라면, 템플릿을 쓰지 마시고, int에 대한 래퍼클래스를 만들고, 그 클래스의 생성자를 explicit으로 선언하시면 될것 같습니다.

hsnks100의 이미지

template 왼꺽쇠 int T 오른꺽쇠
struct test
{
test()
{
_tprintf(_T("TEST"));
}
};

int main(int argc, char* argv[])
{

test왼꺽쇠3.5오른꺽쇠();
return 0;
}

답변이 없네요;; 휴가때 보겠습니다.
----------------------------------------------------
알디 입니다.

----------------------------------------------------
개인 블로그: https://kangssu.com

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.