Wouter van Ooijen
Wouter van Ooijen

Reputation: 1039

pass a value of a derived type as template NTTP parameter

Should it be possible to pass a value of a derived type as NTTP?

struct base { };
struct derived : base { };
template< base name > struct component { };
struct test1 : component< ( base() ) >  { }; // OK
struct test2 : component< ( derived() ) >  { }; // error

C++20 (GCC 10.2) complains

error: conversion from 'derived' to 'base' in a converted constant expression

Upvotes: 1

Views: 58

Answers (1)

Nicol Bolas
Nicol Bolas

Reputation: 474116

The definition of "converted constant expression" (which NTTP arguments must be) specifically excludes the possibility of derived-to-base conversions in the set of allowed standard conversions. This appears to be an oversight, as there's a defect report on it.

So the C++20 standard at present doesn't allow them, though the intent is to fix it to make it possible. In the meantime, if you need to do this, you need an explicit conversion.

Upvotes: 1

Related Questions