jeudi 29 octobre 2015

In-class initialisers using = for class templates

I apologise but I cannot understand why the following will not work (g++ 4.8.1):

#include <string>

using namespace std;

template <typename T> struct A{
    //A(): s("why") { } //fine
    //string s{"what"}; //also fine
    //A() = default; //(same error as below)
    string s = "why?!"; //error: conversion from 'const char [6]' to non-scalar type 'std::string {aka std::basic_string<char>}' requested|
};

struct B{
    string s = "why?!"; //all good
};

int main(){
    A<int> a;
    B b;
}

For some reason by introducing a template I'm unable to use = for the in-class initializer of the string s. Built-in types work and indeed I'm able to circumvent it by using braces or explicitly intialising in the default constructor. Why does it kick up a fuss about using =?

Aucun commentaire:

Enregistrer un commentaire