2005년 6월 7일 화요일

C++ template에서는 string literal을 쓰면 안됨

$ cat ./temp1.cpp
#include <iostream>

extern const char *FOOD_PREFIX = "/tmp/really_hungry"

template<size_t SIZE=8192, const char *PREFIX=FOOD_PREFIX>
class Food
{
    public:
    int m_price[SIZE];
    char *m_prefix;

    Food(const char* prefix=PREFIX) : m_prefix(prefix)
    {
    }
};

int main()
{
    Food<> goodFood;

    goodFood.m_price[0] = 100;

    return 0;
}

[ilashman@ob test_shm2]$ g++ ./temp1.cpp -g -Wall
temp1.cpp: In function `int main()':
temp1.cpp:17: string literal "/tmp/really_hungry" is not a valid template argument because it is the address of an object with static linkage
temp1.cpp:17: ISO C++ forbids declaration of `goodFood' with no type
temp1.cpp:19: request for member `m_price' in `goodFood', which is of
   non-aggregate type `int'

C++ 표준보기

http://library.n0i.net/programming/c/cp-iso/template.html
14.3.2 -2-을 보면
template에서는 string literal을 쓰면 안됨.

댓글 없음:

댓글 쓰기